using sed but want to drop last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed but want to drop last line
# 15  
Old 01-22-2009
Quote:
Originally Posted by vgersh99
why exactly do you need to 'cat' into 'tr' and 'sed'?
Thanks for challenging that. As you already know (since I've seen you issue this challenge before ;-) ), I don't have to cat into either command, when I can use redirect for 'tr' and sed will work on a specified file. Saves a couple of processes.

Also, thanks for fixing my code tags before. I was trying to do that, and then saw it was already done.

So the 'tr' line can be changed to:

Code:
 
tr -s '\012' ' ' < $FILE1 > $FILE2

and the sed line can be changed to:

Code:
 
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' $FILE2 | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MW][EX]/P'

# 16  
Old 01-22-2009
I don't want anyone to think I'm ignoring posts, but I got pulled into an all-day meeting and it may last through tomorrow. Next week I'm traveling (Hawaii, yeah!) so won't be able to test anything on a Linux box. I will still try things on my Windows laptop, but as I've already found, what works here may not work there.Smilie

I really appreciate all the help, and I'm sure we'll come up with a solution. I'm excited to try using awk instead of sed, and I have it on my laptop as well. Please keep the suggestions coming!
# 17  
Old 02-03-2009
I may have it!

Back from Hawaii, leave for Florida tomorrow. Jacksonville is as cold as Seattle right now!Smilie

I did some testing today (on Linux), and I think I have what I need. This is what I did:

Code:

cat 15.txt | tr -s '\012' ' ' > tmp.txt

echo WEATHER: > tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [WX:]/P' >> tmp3.txt
echo EQUIPMENT: >> tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [EQ:]/P' >> tmp3.txt
echo METERING: >> tmp3.txt
echo
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' tmp.txt | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MET][EX]/P' >> tmp3.txt
rm tmp.txt

If I am following the flow correctly, after the first cat changes the newlines to spaces and saves into a temp file, I can then scan for my keywords to retrieve the individual entries required. On the first two I scan for WX: and EQ: and it returns exactly what I want Smilie.

The 3rd time through looking for METERING: I have problems. For some reason it returns the metering lines, plus more that don't match the string. However, I add in the [EX] and then it works! That confuses me, since I have a few more keywords to search for, and I don't want to have to ask for help every timeSmilie

Thanks again for the help. I'll be working on this again next Monday when I return from Florida.

To bad all this travel is work related and I can't spend some time looking around! Smilie
# 18  
Old 02-03-2009
I think this'll work:

Code:
more +/WX: file_in |
sed -e 1p -e '/^.....TMU/d'

# 19  
Old 02-03-2009
Quote:
Originally Posted by atc98092
Back from Hawaii, leave for Florida tomorrow. Jacksonville is as cold as Seattle right now!Smilie

You mean it's tolerable?
Quote:
I did some testing today (on Linux), and I think I have what I need. This is what I did:

Code:

cat 15.txt | tr -s '\012' ' ' > tmp.txt

echo WEATHER: > tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [WX:]/P' >> tmp3.txt
echo EQUIPMENT: >> tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [EQ:]/P' >> tmp3.txt
echo METERING: >> tmp3.txt
echo
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' tmp.txt | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MET][EX]/P' >> tmp3.txt
rm tmp.txt

If I am following the flow correctly, after the first cat changes the newlines to spaces and saves into a temp file,

cat doesn't change anything; it is useless in this context.

Bloated code is hard to read. First, get rid of all instances of cat.

Then get rid of all the >> tmp3.txt and redirect an entire block, e.g.:

Code:
{
  sed ....
  echo ...
  sed ...
  ...
} > tmp3.txt

That way, you can easily comment out the redirection when testing, and you will see the output in your terminal.
Quote:
I can then scan for my keywords to retrieve the individual entries required. On the first two I scan for WX: and EQ: and it returns exactly what I want Smilie.

The 3rd time through looking for METERING: I have problems. For some reason it returns the metering lines, plus more that don't match the string.

There is no search for METERING in your code.

It's very difficult to debug code that you don't show.
Quote:
However, I add in the [EX] and then it works! That confuses me, since I have a few more keywords to search for, and I don't want to have to ask for help every timeSmilie
# 20  
Old 02-03-2009
Quote:
Originally Posted by atc98092
Back from Hawaii, leave for Florida tomorrow. Jacksonville is as cold as Seattle right now!Smilie
Yeah, but you were in Hawaii ... Smilie

Quote:
I did some testing today (on Linux), and I think I have what I need. This is what I did:

Code:
cat 15.txt | tr -s '\012' ' ' > tmp.txt
 
echo WEATHER: > tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [WX:]/P' >> tmp3.txt
echo EQUIPMENT: >> tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [EQ:]/P' >> tmp3.txt
echo METERING: >> tmp3.txt
echo
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' tmp.txt | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MET][EX]/P' >> tmp3.txt
rm tmp.txt

If I am following the flow correctly, after the first cat changes the newlines to spaces and saves into a temp file, I can then scan for my keywords to retrieve the individual entries required. On the first two I scan for WX: and EQ: and it returns exactly what I want Smilie.

The 3rd time through looking for METERING: I have problems.
Yeah, but you were in Hawaii ... oh I said that already! Smilie

Quote:
For some reason it returns the metering lines, plus more that don't match the string.
That's because you're using the bracket expression incorrectly. You've put 'MET' in the bracket expression thinking that will give you lines with 'METERING' in there, but in reality it will give you lines that have an 'M' 'E' OR 'T' in that character position. So you'll get 'Metering' or 'METERING' but you'll also get 'Equipment' and 'EQUIPMENT' and if you have words like 'Time' or 'TUNDRA' or 'The' or you get the picture! Smilie

Quote:
However, I add in the [EX] and then it works! That confuses me, since I have a few more keywords to search for, and I don't want to have to ask for help every timeSmilie
Because the second bracket expression is for the second character position in that word, so you still could conceivably get things other than just 'METERING' but if you have '[MET][EX]' your combinations for those two character positions are:

ME
MX
EE
EX
TE
TX

If the word in that position doesn't start with the above you don't get it. If it does, you do.

Quote:
Thanks again for the help. I'll be working on this again next Monday when I return from Florida.

To bad all this travel is work related and I can't spend some time looking around! Smilie
Yeah, but you were in ... oh forget it! Smilie

Last edited by rwuerth; 02-03-2009 at 06:05 PM.. Reason: (hopefully) clarified some statements.
# 21  
Old 02-03-2009
Quote:
Originally Posted by rwuerth
Yeah, but you were in Hawaii ... Smilie

Yeah, but you were in Hawaii ... oh I said that already! Smilie

Yeah, but you were in ... oh forget it! Smilie
I needed a good chuckle today! Smilie Thanks!

And thanks for the explanation for what those nasty brackets were doing. That means the first two scans were just lucky that nothing else matched that request. I got the flick now!

Quote:
Originally Posted by cfajohnson
You mean it's tolerable?
Mr Johnson:

Tolerable is in the mind of the beholder Smilie!

You're right, my code is really sloppy. I like the idea of blocking the code, didn't even occur to me Smilie. Also letting the results return to the terminal will help a lot. Where I thought I was looking for the METERING keyword [MET] I now understand was only looking for words with the first character matching any of those three letters. That explains why I got the results I did.

As soon as I get back Monday I'll clean it up and use your suggestions. I see now I'm almost there, with all the great advice here. Thanks!

Last edited by atc98092; 02-03-2009 at 06:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

5. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

7. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

8. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

9. Shell Programming and Scripting

print option - drop 1 line

Hi, Is there a print option to drop the data 1 line? Basically the page is too close to the top of the page and I would like to drop it one line automatically, editing the actual data is complicated. (8 Replies)
Discussion started by: mcclunyboy
8 Replies

10. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies
Login or Register to Ask a Question