How to add line to previous line if not a number?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add line to previous line if not a number?
# 8  
Old 01-17-2016
Assumption:
Any line that starts with spaces and the previous one does not start with a number is an anomaly not intended, created by the import of the pdf.

Please, try the following:
Save as fix.pl
Code:
#!/usr/bin/env perl -w
#
use strict;

my $head = <>;
while(<>){
    chomp $head if(/^\s/ and $head =~ /^\D/);
    print $head;
    $head = $_;
}
print $head;

Run as perl fix.pl carlr.txt > fixed.txt
# 9  
Old 01-18-2016
I would augment the previous sed solution with a loop, and replace the line end with a space.
Code:
sed -e '/^[^0-9]/{' -e ':L' -e '$!N; s/\n\([^0-9]\)/ \1/; t L' -e '}' file

As multi-line
Code:
sed '
/^[^0-9]/{
  :L
  $!N
  s/\n\([^0-9]\)/ \1/
  t L
}
' file

Note: $!N is required by a standard sed, otherwise it does not print he last line.
In a recent GNU sed just N is sufficient. But $!N works as well.

Last edited by MadeInGermany; 01-18-2016 at 07:31 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 01-18-2016
Two basic awk things to think of in this kind of situation:
  1. If the default FS is used, test $1 instead of $0. That way you are not dependent on leading spaces that may or may not be there.
  2. If you must use getline, use it conditionally while testing if the return code is >0. For example: if((getline a)>0)
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question