Sed - merge lines bw 2 specific characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed - merge lines bw 2 specific characters
# 8  
Old 03-30-2011
both the sed and awk solutions work great for combining the lines, but it also combined some of my other data.

Is there a way to only run the sed command on lines that start with "tagid" ?

This is the output I got using sed or awk.

Code:
ip = 10.1.2.3 clientType = rtu clientVersion = 1.2 regFlags in binary = 101111 C Entitled = true C Display = true is DT = true is H = true is Home = false retry count = 1 boot reason = CLIENT_REFRESH boot time = 1300800166 htype = rtu registration time = 1300886801 bId = 1 stId = 0 tag count = 27 tagId=2, length=6, value=[ 30 30 30 32 44 45 ]
tagId=3, length=4, value=[ 11 22 33 12 ]
tagId=1, length=2, value=[ 00 65 ]
tagId=16, length=2, value=[ 00 00 ]
tagId=32, length=1, value=[ 63 ]
tagId=33, length=1, value=[ 04 ]
tagId=34, length=1, value=[ 00 ]
tagId=35, length=16, value=[ 00 04 00 13 88 3f 00 13 00 00 00 00 4d 86 95 00  ]
tagId=35, length=16, value=[ 00 04 00 0a 80 e7 00 ef 00 00 00 00 4d 86 95 00  ]
tagId=35, length=16, value=[ 00 04 00 0b 80 84 00 88 00 00 00 00 4d 86 95 00 ]
tagId=35, length=16, value=[ 00 04 00 13 53 95 00 0f 00 00 00 00 4d 86 95 00  ]
tagId=35, length=16, value=[ 00 04 00 0a 67 b7 00 bc 00 00 00 00 4d 62 fc 80  ]
tagId=35, length=16, value=[ 00 04 00 11 ac d9 00 29 00 00 00 00 4d 62 fc 80  ]
tagId=35, length=16, value=[ 00 04 00 11 f7 ea 00 1c 00 00 00 00 4d 62 fc 80  ]
tagId=35, length=0, value=[ ]
tagId=35, length=0, value=[ ]
tagId=35, length=0, value=[ ]
tagId=48, length=4, value=[ 00 00 00 09 ]
tagId=49, length=1, value=[ 00 ]
tagId=64, length=4, value=[ 00 00 00 1f ]
tagId=65, length=1, value=[ 01 ]
tagId=80, length=2, value=[ 00 00 ]
tagId=81, length=2, value=[ 00 00 ]
tagId=144, length=2, value=[ 00 00 ]
tagId=4, length=1, value=[ 01 ]
tagId=128, length=9, value=[ 11 22 33 2e 38 27 34 31 87 ]
tagId=129, length=18, value=[ 11 22 33 44 31 2e 35 2e 21 5f 76 2e 70 aa 87 38  32 98 ]

needed the rest of the data to stay on separate lines like the following:

Code:
ip = 10.1.2.3 
clientType = rtu 
clientVersion = 1.2 
regFlags in binary =  101111 
C Entitled = true 
C Display = true is 
DT = true is 
H = true is  
Home = false 
retry count = 1
boot reason = CLIENT_REFRESH 
boot time =  1300800166 
htype = rtu registration time = 1300886801 
bId = 1 stId = 0  tag count = 27 
tagId=2, length=6, value=[ 30 30 30 32 44 45 ]
tagId=3, length=4, value=[ 11 22 33 12 ]
tagId=1, length=2, value=[ 00 65 ]
tagId=16, length=2, value=[ 00 00 ]
etc....

You guys are awesome btw, this is way beyond my skill level with UNIX

---------- Post updated at 10:58 AM ---------- Previous update was at 10:44 AM ----------

Or perhaps a simpler method would be to search for "ip =" and then reconvert that entire line into separate lines?

---------- Post updated at 11:27 AM ---------- Previous update was at 10:58 AM ----------

Fixed it with some sed commands.

Used sed '5,${:a;/]/!N;s/\n/ /;ta}' then used some more sed commands to add newlines to the long merged line.

Thanks all for your help!

Last edited by Scott; 03-30-2011 at 02:50 PM.. Reason: Code tags for code, quote tags for quotes
# 9  
Old 03-30-2011
Quote:
Originally Posted by Winsarc
Is there a way to only run the sed command on lines that start with "tagid" ?
Sounds like you might already have it fixed, but here is a change to start from the tagId line for sed/awk:

Code:
sed '/^tagId/,${:a;/]/!N;s/\n/ /;ta}' file
awk '/^tagId/{a=1}!a{print;next}{printf $0 (/]/?RS:" ")}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge lines with varying characters

Hi, I have a large set of data (firewall logs) that I'm trying to summarize. I've been able to write a script to consolidate the ports, now am looking to conslidate even further, based on IP. Source Destination Type Port 192.168.5.108 192.168.11.12 TCP 1, 2, 3, 4, 5, 15 192.168.5.109... (6 Replies)
Discussion started by: umang2382
6 Replies

2. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Replacing specific characters using sed

Hi, I have a text file which is output from a server and it lists all the files in a specific volume. However, the volume name appears as volume_name:. I would like to replace this with \\volume_name\volume_name. This is not a problem in itself as I can use sed to globally look for the... (8 Replies)
Discussion started by: vnayak
8 Replies

4. Shell Programming and Scripting

How delete characters of specific line with sed?

Hi, I have a text file with some lines like this: /MEDIA/DISK1/23568742.MOV /MEDIA/DISK1/87456321.AVI /MEDIA/DISK2/PART1/45753131.AVI /IMPORT/44452.WAV ... I want to remove the last 12 characters in each line that it ends "AVI". Should look like this: /MEDIA/DISK1/23568742.MOV... (12 Replies)
Discussion started by: inaki
12 Replies

5. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

6. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

7. Shell Programming and Scripting

Merge two lines using sed

Hi, I am trying to merge two lines, first line starts with a particular pattern and second line ends with a particular pattern in a file. Something like: First line starts with say ABC Second line ends with say XYZ After a merge, the line should become ABC.......XYZ I tried... (14 Replies)
Discussion started by: Sunny Arora
14 Replies

8. UNIX Desktop Questions & Answers

grep lines with two specific characters somewhere in the line

I'm having trouble with extracting certain lines from a file based on whether they have all the required fields. Original file: snt:594:Sam N This bpt:2342:Bob P That lr:123 wrp:23:Whoever Person cor:794 Desired output: snt:594:Sam N This bpt:2342:Bob P That wrp:23:Whoever Person ... (3 Replies)
Discussion started by: Chthonic
3 Replies

9. Shell Programming and Scripting

Merge lines in Flat file based on first 5 characters

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds... (3 Replies)
Discussion started by: Brado
3 Replies

10. Shell Programming and Scripting

Use sed to merge multiple lines

Hi all: I have a file in which the contents are as following: ... This is a test ONE TWO Hello, world! XXX YYY CCC test again three, four five six seven world AAA BBB QQQ test eight, nine world (3 Replies)
Discussion started by: xb88
3 Replies
Login or Register to Ask a Question