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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add the line to previous line in | delimited text?
# 8  
Old 07-08-2016
Proposal applied to your sample in post#7 (which, btw, doesn't differ from the ones in post#1):

Code:
awk '{printf "%s%s", NF==5?RS:"", $0} END {printf RS}' FS="|" file
41|216|398555|77|provided complete NP outcome data constituted
72|192|402632|580|Completed OHTS Phase 2,through March

I can't see that (and why) it should concatenate everything into one single line...

To remove the surplus spaces as requested in post#1, I added a gsub call:
Code:
awk '{gsub (/  */, " "); printf "%s%s", NF==5?RS:"", $0} END {printf RS}' FS="|" file

# 9  
Old 07-09-2016
The point is to replace the condition /[|]/ that means NF>=2 by the required one, NF==5 or NF>=5
# 10  
Old 07-11-2016
Sorry to all for not using the codes.I used the code tag now.Please let me know still I need to do any changes while posting in the forum.

Thanks RudiC for your command,

I used your commands and working fine but after executing it is deleting few lines in my original file.I took the deleted lines from the original file and kept it in test file and I executed the command but it is not changing any thing I am attaching the data for your reference.

I tried below commands on test file and data is not changing.

Code:
awk '{printf "%s%s", NF==8?RS:"", $0} END {printf RS}' FS="|" test_11.txt > test_12.txt

awk '{gsub (/  */, " "); printf "%s%s", NF==8?RS:"", $0} END {printf RS}' FS="|" test_11.txt > test_12.txt

Thanks,
Narasimhatest_11.txt
# 11  
Old 07-11-2016
That's a different story - why didn't you post representative samples in the first place? Try
Code:
awk '{while (NF < 8) {getline X; $0 = $0 " " X}}1' FS="|" /tmp/test_11.txt

.

BTW, your file contains DOS line terminators (<CR>, \r, 0x0D) which you might want to get rid of before text processing in *nix.
# 12  
Old 07-11-2016
Thanks RudiC,

As per your update I want work on DOS line terminators before processing/using the commands which you posted.Could you please let me know how to to work on this DOS line terminators in Unix.

Thanks,
Narasimha
# 13  
Old 07-11-2016
Quote:
Originally Posted by Narasimhasss
As per your update I want work on DOS line terminators before processing/using the commands which you posted.
A word in general: we are a self-help forum! That means: we help you to help yourself, we are not doing your work for you. If you want that: hire someone.

Here is how you do it in sed (note that "^M" is a single character! You enter it i.e. in vi pressing <CTRL>-<V> and then the <ENTER> key):

Code:
sed 's/^M$//' /path/to/file

I hope this helps.

bakunin
# 14  
Old 07-13-2016
Hi bakunin,

Thanks for your commands.

Instead of using Unix commands I have opened the file which is having pipe delimiter in Notepad++
and every line is having CRLF and only LF.
whereever we have single LF we are facing issue.To avoid the issue, I have replaced the individual value LF with CR then i exported the file into Unix server.After that It is working fine.

But I have one file which is having 850 MB (89,18,51,027 bytes) in size and i could not able to open in Notepad++.If we use the Unix command to do the same fix for this file then issue will resolve.

I used command which you have suggested and other commands to but not working as expected.
Code:
sed 's/^M$//' /path/to/file

Could you please let me know is there any way to do so.

Thanks,
Narasimha
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 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

4. Shell Programming and Scripting

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

Hi, I am trying to compare 2 lists. However, one of these lists has to be taken from a.pdf file. When I copy the test into a .txt document there are formatting errors which I need to correct. The document is long (~10,000 lines) so I need to script the re-formatting. Currently my file looks... (9 Replies)
Discussion started by: carlr
9 Replies

5. UNIX for Dummies Questions & Answers

How to change a line of text to a comma delimited string?

Hi, Is there a one-liner that I can use to change a line of text into a comma delimited string? For example, convert user1 user2 user3 user4to user1,user2,user3,user4Currently using while read x, although got the extra comma at the end that I have to remove manually. Please... (5 Replies)
Discussion started by: newbie_01
5 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. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 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

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

10. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies
Login or Register to Ask a Question