jyotib
January 16, 2008, 2:06pm
1
I need a help in getting substring of each line in input file.
I am writing a script that will read a file from a directory on daily basis, I mean everyday a new file will be stored in this directory, it will replace old file. I have to read contents of this file, the contents will be as follows:
1090373422,4024901970
1090373422,4025158069
1090373422,4025643181
I have to read these contents line by line and then display them as 1090373422_4024901970.jpg
1090373422_4025158069.jpg
1090373422_4025643181.jpg
I don't know how to get a substring of each line.
Thank you,
Jyoti.
while read line;do echo ${line//,/_}.jpg;done < file.txt
It can be simple and it might be needed to do some checks on the data.
In this example I used grep to make sure it is a line with a comma and
assuming it is a line with data to be converted.
grep , inputfile | tr , _ | while read LINE; do echo $LINE.jpg; done
Or with awk:
awk -F, '{ print $1_$2".jpg" }' inputfile
If you want to make it more flexible I would use "while read ...."
cat inputfile | while read LINE
do
.....
done
--- I hope this answers your question ---
-Ren�
jyotib
January 16, 2008, 2:42pm
4
it gives an error
readline.sh[23]: ${data//,/_}.jpg: bad substitution
jyotib
January 16, 2008, 3:48pm
5
It worked, Thanks for your help Rene.
Use bash scripting and the script nugget from in2nix4life works great.