Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove last field if string exists 2

Status
Not open for further replies.

pavNell

Technical User
Sep 27, 2002
178
0
0
US
Hello all,
I have a temporary file and at any time it may have only one of the following lines or none at all (empty file).

AAA 50 50 60 60 90 90
or
AAA 60 60 60 60 80 80 newfile:/path/to/a/file
or
newfile:/path/to/a/file

Needing an awk in my shell script to remove only the "newfile:/path/to/a/file" string if it exists. There are no spaces in the string. I used sed and it works fine except that if sed does not find the string pattern (empty file) it inserts a blank line in the file. That causes problems down the road. Yes, i'm sure I could just use another command to remove blank lines, but for the sake of efficiency, would rather not.

the sed that I used is
sed 's/newfile.*//' /tmp/tmpfile.tmp
Perhaps it could be modified as well. Trying awk this time.
Thanks for any help.
 
Hi

[tt]sed[/tt] should work. Maybe try :
Code:
sed '/newfile/s/newfile.*//' /tmp/tmpfile.tmp
But while you ask for [tt]awk[/tt] :
Code:
awk '{sub(/newfile.*/,"")}1' /tmp/tmpfile.tmp

[gray]# or[/gray]

awk '/newfile/{sub(/newfile.*/,"")}1' /tmp/tmpfile.tmp

Feherke.
 
thanks, but your examples still leave a blank line *IF* the line is just 'newfile:/path/to/file'. They do work on the other lines however.

But I've figured it out with sed!
sed -e 's/newfile.*//' -e '/^[ ]*$/d' /tmp/tmpfile.tmp

the second sed expression removes the blank line(s).

So using the above command and to also overwrite the input file, the two lines in my bash script....
line=`sed -e 's/newfile.*//' -e '/^[ ]*$/d' /tmp/tmpfile.tmp 2>/dev/null`
echo "$line" > /tmp/tmpfile.tmp

thanks for the help.

 
An awk way:
awk '{sub(/newfile.*/,"")}NF' /tmp/tmpfile.tmp > /tmp/tmp$$ && mv /tmp/tmp$$ /tmp/tmpfile.tmp

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,
Yes, that works also. The awk example feherke gave is very similar to yours too. However, your example causes two writes to disk, right? Or is the first cached? I'm probably being a bit too anal about it but my two liner only causes one write and therefor is the more efficient solution in my opinion. But I'm no guru so feel free to debunk my logic if I'm not correct.

Cheers!
 
Another one liner:
echo "1,\$s/newfile.*//\ng/^ *\$/d\nw" | ed -s /tmp/tmpfile.tmp

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Regarding my last post. I was wrong. My two liner still adds a blank line to my tmp file. Just wanted to clear that up give credit where it's due.

PHV's right on the money with his examples.
 
PHV,
WOW!
my heads hurts trying to read that last one-liner. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top