Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-28-2005, 01:21 PM
|
#1
|
Member
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100
Rep:
|
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
|
|
|
07-28-2005, 02:03 PM
|
#2
|
Member
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100
Original Poster
Rep:
|
additionally
additionally, all non- blank lines do contain at least 1 period.
|
|
|
07-28-2005, 02:28 PM
|
#3
|
Member
Registered: Apr 2001
Location: Bremen, Germany
Distribution: Debian
Posts: 303
Rep:
|
awk '{if(NF!=0)print "-a",$0; else print $0}' infile
|
|
|
07-28-2005, 05:02 PM
|
#4
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,258
|
For almost any sort of task involving line-oriented data, awk is your best friend. Definitely an important tool to know.
|
|
|
08-01-2005, 01:45 PM
|
#5
|
Member
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100
Original Poster
Rep:
|
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
|
|
|
08-01-2005, 01:52 PM
|
#6
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,258
|
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.
|
|
|
08-02-2005, 10:17 AM
|
#7
|
Member
Registered: Apr 2001
Location: Bremen, Germany
Distribution: Debian
Posts: 303
Rep:
|
*hmmms*
Code:
luna:~/tmp demian$ wc -l data.txt
3200 data.txt
luna:~/tmp demian$ time ./yourscript.sh
head: illegal line count -- 0
real 3m50.525s
user 0m22.550s
sys 1m30.890s
luna:~/tmp demian$ time awk '{if(NF!=0)print "-a",$0; else print $0}' data.txt > goodfile2.txt
real 0m0.098s
user 0m0.014s
sys 0m0.012s
luna:~/tmp demian$ diff goodfile.txt goodfile2.txt
luna:~/tmp demian$
|
|
|
01-01-2006, 07:19 PM
|
#8
|
Member
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100
Original Poster
Rep:
|
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
|
|
|
01-01-2006, 08:08 PM
|
#9
|
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290
|
Another possibility would have been to use sed -- something like:
Code:
sed 's/^\(.*\..*\)/-a \1/g'
ought to work if each line you want prefixed has a period in it.
|
|
|
All times are GMT -5. The time now is 07:42 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|