No offense intended, but that's a terrible solution. There are quite a few bugs in that short script.
First of all, we don't know anything about the data, so we can't make any assumptions.
If there is leading or trailing whitespace, the field splitting done by read will discard them. This will affect the results of the parameter expansion, yielding characters that begin later in the line than desired, and/or we could miss characters at the end of the substring if they were discarded whitespace.
The read does backslash escaping. If there are backslashes in the data, again, an incorrect substring is the result.
If the correct substring is extracted, it could still fail to print properly if it looks to echo like a valid option or valid escape sequences.
What if there's an asterisk, a question mark, or a bracketed expression? Those may trigger pathname expansion (aka file globbing) since the parameter expansion is unquoted.
Troublesome sample data:
If you wanted to do this correctly with bash builtins and parameter expansion, the following is the way:
Ygor's suggestion is probably simplest and best.
Regards,
Alister
Last edited by alister; 03-15-2013 at 03:16 PM..
These 2 Users Gave Thanks to alister For This Post:
Hello,
A question please.
A have a file that contains a string. Ex:
AAAABBCCCCCDDEEEEEEEEEEFF
I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file.
From position 5, 2 caracters (ex:"BB") and from position 25, 2 caracters (ex:"FF") in a file.
Could anoyone help me... (3 Replies)
Hello,
A question please.
A have a file that contains a string. Ex:
AAAABBCCCCCDDEEEEEEEEEEFF
I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file.
Could anoyone help me please?
Thanks in advance (3 Replies)
I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.)
I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
I am new to awk and writing a script using awk. I have file containing fixed length records, I wish to extract 2 substring(each substring is padded with zeros on left e.g 000000003623) and add each substring respectively for every record in the file to get total sum of respective substring for all... (5 Replies)
Hi Guys,
While I was writing one shell script , I just got struck at this point.
I need to extract words from a file at some specified position and do some comparison operation and need to replace the extracted word with another word.
Eg : I like Orange very much.
I need to replace... (19 Replies)
This is the line that I am using:
sed 's/^*\({3}*$\)/\1 /' <test.txt >results.txt
and suppose that test.txt contains the following lines:
http://www.example.com/200904/AUS.txt
http://www.example.com/200903/_RUS.txt
http://www.example.com/200902/.FRA.txt
What I expected to see in results.txt... (6 Replies)
I have a string:
hgLogOutput=" +0000 files: forum/web/hook-test.txt /forum/web/hook-test-2.txt description: test"
and I want to extract the file names from it, they will always appear between the files: and the description:. I have worked out that I can do this:
"$hgLogOutput" | awk '{... (2 Replies)
hi all, I'm really newbie on this and I need some help.
how is the best way to extract a strig or substring from a each line in a file.
e.g. I want to print only this ERROR=JUD+the followed numbers from one line like this one, considering the numbers change related to different errors
... (1 Reply)