Dear Experts
I think this is possibly the easiest thing. but I am not able to solve:
I need comma to be added to end of each line echo'd. But does not want it to be added to last line.
I have a script which does some data analysis and creates a command as in below code snippet
for file in $(sort -u -k3nr data | cut -d, -f2| head -5 | tr -d ' ')
do
echo "\"<grep -w $file data\" using 1:3 title '$file' with linespoints ,\\"
done
Now output I get is
"<grep -w dir1 data" using 1:3 title 'dir1' with linespoints ,\
"<grep -w dir23 data" using 1:3 title 'dir23' with linespoints ,\
"<grep -w whatever data" using 1:3 title 'whatever' with linespoints ,\
Now I don't want to have comma and slash on last line.
I don't know how many sort command will pull out. and also don't want to have a command wc -l to calculate before hand. I have been trying echo -e "\b" without
luck ..
any other suggestions
Hi,
Try this..!!
This will help you when you write all the commands which you created using a for loop to a file.
Assuming you modify your code as:
echo "\"<grep -w $file data\" using 1:3 title '$file' with linespoints ,\\" >> cmd.txt
Use the below code to remove ,\ from last line
sed '$s/,\\$//' cmd.txt
Thank you . This is really good.
Usually I dont like to create a temp file. Had a bad experience
I am keeping everything in variable and want to execute the command.
Even though I said I dont want to use wc -l .. For now I am doing
nodir=$(sort -u -k3nr data | cut -d, -f2| head -5 | tr -d ' '|wc -l)
and comparing
[[ $nodir > 0 ]] && echo ",\\"
[[ $nodir == 0 ]] && echo " "
for file in $(sort -u -k3nr data | cut -d, -f2| head -5 | tr -d ' ')
do
echo "\"<grep -w $file data\" using 1:3 title '$file' with linespoints ,\\"
done | sed '$s/...$//'
result=$(
for file in $(sort -u -k3nr data | cut -d, -f2| head -5 | tr -d ' ')
do
echo "\"<grep -w $file data\" using 1:3 title '$file' with linespoints ,\\"
done
)
echo "${result%???}"
Wow .. I knew I asked at right place ..
Thanks a ton guys ..
I like sed .. using sed method
sed '$s/...$//'
or what asterisk gave
sed '$s/,\\$//'