Sponsored Content
Top Forums Shell Programming and Scripting How to add line to previous line if not a number? Post 302964656 by Don Cragun on Sunday 17th of January 2016 06:40:43 PM
Old 01-17-2016
Hi Ravinder,
Note that your code is adding a field separator when joining alphanumeric data lines. (I'm guessing this is because carlr didn't use CODE tags when presenting the sample input and output files and you copied the sample data before Scrutinizer edited that post to include the tags that made the <space> at the start of the line that needed to be joined visible.)

Hi Scrutinizer,
I like your suggested awk script and it works perfectly for the sample data given. Unfortunately, the sample data carlr provided doesn't agree with the description of the actions to be taken:
Quote:
Currently my file looks like:


Code:
862.0
DIAPHRAGM, WITHOUT MENTION OF OPEN WOUND INTO CAVITY
862.1
DIAPHRAGM, WITH OPEN WOUND INTO CAVITY
862.3
OTHER SPECIFIED INTRATHORACIC ORGAN, WITH OPEN WOUND INTO CAVITY
862.8
MULTIPLE AND UNSPECIFIED INTRATHORACIC ORGANS WITHOUT MENTION OF
 OPEN WOUND INTO CAVITY
863
INJURY TO GASTROINTESTINAL TRACT
863.0

But i want it to look like the below: i.e alternating between a line of didgets and a line of text. I believe that there should eb a way of making a SED or AWK statement which looks at each line, and if there are 2 consecutive lines starting with a letter A-Z, moves the second of these to be after the first.
Note that the line shown in red does not meet the requirement shown in red. The line to be combined starts with a <space>; not an uppercase alphabetic character. Your code compensated for that inconsistency by looking for a non-digit instead of looking for an uppercase alpha.

I'm guessing that what carlr really wants to do is join any lines that contain anything other than digits and a possible decimal point. This would allow input like:
Code:
862.8
MULTIPLE AND UNSPECIFIED INTRATHORACIC ORGANS WITHOUT MENTION OF
 OPEN WOUND INTO CAVITY
862.9
2 OR MORE AND UNSPECIFIED INTRATHORACIC ORGANS WITHOUT MENTION O
F OPEN WOUND INTO CAVITY
863
INJURY TO GASTROINTESTINAL TRACT

to be turned into:
Code:
862.8
MULTIPLE AND UNSPECIFIED INTRATHORACIC ORGANS WITHOUT MENTION OF OPEN WOUND INTO CAVITY
862.9
2 OR MORE AND UNSPECIFIED INTRATHORACIC ORGANS WITHOUT MENTION OF OPEN WOUND INTO CAVITY
863
INJURY TO GASTROINTESTINAL TRACT

instead of the output your script produces:
Code:
862.8
MULTIPLE AND UNSPECIFIED INTRATHORACIC ORGANS WITHOUT MENTION OF OPEN WOUND INTO CAVITY
862.9

2 OR MORE AND UNSPECIFIED INTRATHORACIC ORGANS WITHOUT MENTION O
F OPEN WOUND INTO CAVITY
863
INJURY TO GASTROINTESTINAL TRACT

If input like this is a concern to the submitter, something like this (that I had created before I saw Scrutinizer's suggestion):
Code:
awk '
{	d2 = ($0 ~ /[^0-9.]/) ? "" : ORS
	d1 = (d2 && NR > 1) ? d2 : ""
	printf("%s%s%s", d1, $0, d2)
}
END {	if(!d2)	print ""
}' file

or (using Scrutinzer's code as a base):
Code:
awk 'END{if(ORS==x)printf RS} {ORS=x} !/[^0-9.]/{if(NR>1)printf RS; ORS=RS}1' file

might work better.
These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

crontab: error on previous line; number out of bounds.

Hi, I am trying to set up a cron job for every Friday at 6:00 p.m. and got an error: "/var/tmp/aaaa29638" 1 line, 73 characters 00 18 00 0 5 /app/test/backup.ksh crontab: error on previous line; number out of bounds. Any ideas? Thanks! (1 Reply)
Discussion started by: oradbus
1 Replies

2. Shell Programming and Scripting

add number in lines line by line in different files

I have a set of log files that are in the following format ======= set_1 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_2 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_3 ======== counter : 315 counter2: 204597 counter3: 290582 Is... (6 Replies)
Discussion started by: grandguest
6 Replies

3. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

4. 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

5. Shell Programming and Scripting

Choose a line-number to add line

Hello guys, I'm making a script to add visudo with this script. Do you guys know if it's possible to add words to a line-number you want to. Something like this: echo "Adding words to line-number 16" >> /etc/sudoers # (options to add to line-number-16)? Thanks! (3 Replies)
Discussion started by: dannyvdberg
3 Replies

6. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

7. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

8. Shell Programming and Scripting

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

9. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

10. Shell Programming and Scripting

Add previous text when replacing comma with new line

Hi, I've got this output: # cat test2.txt TM1ITP1-TMNLSTP1 SLC00=0,SLC01=0,SLC02=0,SLC03=0 if I just use cat test2.txt | tr "," "\n" I'll end up very near to what I'm trying to achieve: TM1ITP1-TMNLSTP1 SLC00=0 SLC01=0 SLC02=0 SLC03=0 But how can i eventually add the term... (1 Reply)
Discussion started by: nms
1 Replies
All times are GMT -4. The time now is 01:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy