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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed/awk: If you find string1 in line, take string2+ from line move

Status
Not open for further replies.

fixengineer

Technical User
Apr 5, 2007
19
US
I want to display a log file in a more readable format.

The core thing is to look for one string in a line then when found take certain data from that line and insert it above the line. If you found a line with CarType=Race, find out what the tire size is in that line and then copy and insert it above the line. I'm playing around with sed 'i' and 'h' and 'g' and also the markers \(\). No go so far.

Basically I need to say with sed or awk: "look for 'CarType=Race' in file 'cars.dat' and display each line after you check it. When you find 'CarType=Race', search within that line for 'TireSize=AndTheNext3CharactersAfterString'.(In this case '12"'). Hold 'TireSize=AndTheNext3CharactersAfterString' and wildcard characters(...) and insert a space and then 'TireSize=AndTheNext3CharactersAfterString' above line where you found 'CarType=Race'.

The format I want is as such:
-----------------------------

Tire size=12"

CarType=Race TireSize=12" Color=Blue
CarType=Sedan TireSize=14" Color=Black

The file is in this format:
----------------------------
CarType=Race TireSize=12" Color=Blue
CarType=Sedan TireSize=14" Color=Black

Thank you very much for your help on this. Trying to build a tool that will make my work so much easier and efficient. Yes...I'm at it again! ;)
 
Something like:

[tt]awk '/CarType=Race/ {print $2 "\n"} 1' inputfile[/tt]

Annihilannic.
 
This looks like it might be along the lines of what I'm looking for, however it does'nt addres some of the core issues I'm having:

How to copy wildcard strings from lines that meet search parameters.

How to paste both the search pattern itself and the string within the line with search pattern AND a wildcard string from the line.

Fields cannot be used because the fields will not be standard. The string within the line that should be pasted will move from field to field, so you have to search for the string itself, not the field.

We are looking for:

find line with pattern ---- 's/pattern/'

find and hold on that line this string with 3 charactrer wildcard(...) ---- /\(string\).../h'

Paste both pattern and string withing line above line with pattern --- /g\1/i\

I know my syntax is all over the place, but I don't really know it should go, hence my problem. To be honest I think I already know all the pieces to make this work, but I can't fit them together in one statement.
 
Hmm... I'm not sure why you posted here then, if you think you know the individual bits already, and haven't stated the full problem, it's kind of hard to help you. :)

Annihilannic.
 
I thought I went above and beyond stating just the problem. I even showed pieces that do most of the work, so it that requires less work on the park of the person helping me. I'm almost there so you don't have to explain a whole bunch of things to me, just that one final missing piece that I can't figure out.

Again the problem is how do you combine the pieces I have in one statement? My limit is search for pattern on line, then paste the same pattern you searched for above the line. How do I add to that the grab string+wildcard from line where you find pattern.

I even gave a very specific example of precisely what I want to do.

How do you go from a file that has this:
----------------------------
CarType=Race TireSize=12" Color=Blue
TireSize=14" CarType=Sedan Color=Black

-----------------------------
to be that file being DISPLAYED like this...

Tire size=12"

CarType=Race TireSize=12" Color=Blue
TireSize=14" CarType=Sedan Color=Black


I'll even break it down even further what happened above. A search was made in a file with varying fields for string "CarType=Race". When found in line 1, the line was searched for the string "TireSize", when found that string and the next 3 characters were copied and pasted above the line where the strings were found.

In simple terms, we took a file with information about cars and we highlighted the tire size of all the cars that are race cars.

I know about multiple edit(-e). I know about holding text(h). I know about insert above(i), but what I don't know is how to combine it all in one statement; pattern search, hold regular expression, insert regular expression above...how can you make it all work together?
 
The awk solution I posted results in exactly the output you requested, so I'm unsure what you mean by it "not addressing some of the core issues". Is it just that you'd prefer to implement it in sed?

Note that you can combine multiple sed commands after a matching expression by surrounding them in { } braces, maybe that's the missing ingredient?

Something like this perhaps:

[tt]sed '/CarType=Race/{
i\

h
s/.*\(TireSize=[0-9][0-9]*"\).*/\1/
p
i\

x
}' inputfile[/tt]

Annihilannic.
 
Remember I was saying that your command uses fields which won't work cause the fields change. The sate we need will not always be in the second field. I'm assuming $2 means field 2. I tried your command just to be sure and it did'nt work. It just printed out random data(cause field contents are random). Your command does not seem to have any regular expressions either. Remeber we wanted to grab a second pattern plus a regular expression match and insert on the line above. I think you command simply attempts to copy the entire contents of a field reguardless if that field has the correct data or not and insert above.

This curly bracket thing though!? That just might be it! I assume that it funtions similar to "-e". Meaning that everything within the brackets will be done to each line of the file. If so I can search and hold pattern in line 1, then search and hold 2nd pattern in line 2
then use 2 insert statement to get the help patterns and paste them above. This is probably what your new solution does, but I have to try it out. Trying now. Thanks!


awk '/CarType=Race/ {print $2 "\n"} 1' inputfile

The awk solution I posted results in exactly the output you requested, so I'm unsure what you mean by it "not addressing some of the core issues". Is it just that you'd prefer to implement it in sed?


 
This works!! Sweet! The curly brackets is what I needed. It alows me to combine all the commands I already knew into one statement and does it on a line by line basis. This is a great help. I really apreciate the help, this will really make my job so much easier and faster by allowing me to interpret log files much faster by placing key data in a place that is easily visible. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top