Is it possible to send a variable to a sed or cut command? I have a test script as below:
counter=1
while read line
do
# Test the file
printf "$line" > temp$counter
pref=$(cut c1-2000 $temp$counter | sed 's/[^a-zA-Z0-9+_:-]//g' | sed 's|.*PutTime\(.*)Origin.*|\1|')
printf" let counter=counter+1
done < temp01
What I would like is to avoid writing the $line to a file and the passing it to the pref=....... line. What I would like to do is say assign the contents of the $line to a variable, say, record and then use record in the cut/sed command. I can change the first sed with cut i.e. cut after sed instead of doing it first.
counter=1
while read line
do
# Test the file
pref=$(echo $line | cut c1-2000 | sed 's/[^a-zA-Z0-9+_:-]//g' | sed 's|.*PutTime\(.*)Origin.*|\1|')
printf" let counter=counter+1
done < temp01
However, be advised that this sounds like the tail wagging the dog. Reading a line at a time in a while loop and feeding it through a complex pipeline of shell commands with echo is rarely a good solution, and often a sign that you should approach the problem with a few lines of awk or sed which you run directly on the input file.
This is rather unattractive but should hopefully give you an idea of how to proceed. In particular, the repeated gsubs and the substring extraction could probably be optimized if we knew more about the input data format.