Data Processing
Hey Gang,
I got a pretty quick question. I have a file where blank lines are being used as a place marker, so i cannot remove them: ex: train school" "business truck plant wagon I would like to put a switch in front of only non- blank lines, while leaving blank lines intact: ex: -a train -a school" "business -a truck -a plant -a wagon I'm looking for the most simple solution, I am not looking to complicate the script too much as efficiency is a prerequisite. Thanks KARL |
additionally
additionally, all non- blank lines do contain at least 1 period.
|
awk '{if(NF!=0)print "-a",$0; else print $0}' infile
|
For almost any sort of task involving line-oriented data, awk is your best friend. Definitely an important tool to know.
|
Solution
Hey Gang,
This is the solution I ended up going with: ######################################### file=data.txt #sets variable file to data.txt numlines=`wc -l $file | awk '{ print $1 }'` #sets $numlines to the number of lines in $file r=0 #sets $r to 0 ######################################### while [ $r -le $numlines ]; do #runs loop as many times as there are lines in the file line=`cat $file | head -$r | tail -1` # $line is equal to the data on the particular line we ## are on in the loop if [[ $line = *.* ]] #if $line contains anything than a period than anything else then echo "-a" $line >> goodfile.txt # prints -a plus the data previous on that line else echo >> goodfile.txt # if it does not contain anything a period anything else prints a ## blank line to goodfile.txt fi r=`expr $r + 1` #adds 1 to $r done ########################################### Any questions feel free to mail. Thanks for all the help, -Karl |
It will work of course... but you could have done it all with awk. I would encourage you to read-up on it and see how the tool could have been applied to the problem.
|
*hmmms*
Code:
luna:~/tmp demian$ wc -l data.txt |
I did use awk, where needed: numlines=`wc -l $file | awk '{ print $1 }'`
That was all it was needed for, the rest was processed through a while loop. ;p |
Another possibility would have been to use sed -- something like:
Code:
sed 's/^\(.*\..*\)/-a \1/g' |
All times are GMT -5. The time now is 07:49 PM. |