bash: read from character x to y for every line
Hello all,
for the file test.dat, I want to extract the characters at position 7,8 and 9 of each line. It is important to exactly specify the range from 7 to 9. I can't use $4 to pick it via awk. content of file test.dat: Code:
1 2 3 456 789 Code:
456 Kind regards vanKey |
You mean like this ?
Code:
echo '1 2 3 456 789'|cut -c7,8,9 |
Yes, either AWK or Perl will do that quite easily. Please post what you have tried and where you are stuck. Be sure to use [code] [/code] tags around your work to preserve indentation.
|
What I did is
Code:
awk '{print $4}' test.dat What I need is to specify something in awk with reference to the line entry ID's 7-9, that have to be put out. Kind regards vanKey |
Quote:
Check the various guides periodically as you get closer to your goal. Bruce Barnett's AWK tutorial is quite a good one for that. |
If you always want positions 7-9 then all you need is
cut -c7-9 test.dat >> output-file.dat |
I seem to recall advising you to download and read the gawk doco at the link I provided. Do so.
|
Thanks ArfaSmif, that did it!
Kind regards |
Why not pure bash, as the thread title suggests?
Code:
while read line; do |
Note that the read command defaults to some magic:
skipping initial space, special treatment of backslash. You can turn the magic off with Code:
while IFS= read -r line; do |
Code:
txt=" |
All times are GMT -5. The time now is 04:23 PM. |