awk for replacing line feed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk for replacing line feed
# 1  
Old 08-07-2013
awk for replacing line feed

Hello all,
I have data like
Code:
"1"|"My_name"|"My_Email"|"My_Last"|My_other"
"2"|"My_name"|"My_Email"|"My_Last"|My_other"
"3"|"My_name"|"My_Email"|"
"|My_other"
"1"|"My_name"|"My_Email"|"My_Last"|My_other"

Need output like
Code:
"1"|"My_name"|"My_Email"|"My_Last"|My_other"
"2"|"My_name"|"My_Email"|"My_Last"|My_other"
"3"|"My_name"|"My_Email"|""|My_other"
"1"|"My_name"|"My_Email"|"My_Last"|My_other"

and so far i have SED working file but it blowing up the memory if the file exceeds in millions of rows.
and for AWK i have this
Code:
nawk -F"\|" 'NR==1 || $NF !~ /\|\"\n/{printf("%s",$0)} $NF ~ /\|\"\n/{printf("\n%s",$0)} END{print ""}'

Its giving me output all rows in single row.

Please help.

Last edited by Franklin52; 08-08-2013 at 03:20 AM.. Reason: Adding code tags
# 2  
Old 08-07-2013
Assuming all your lines should contain string: My_other
Code:
awk '/My_other/{ORS=RS}!/My_other/{ORS=""}1' file

# 3  
Old 08-07-2013
Actually Not as i just provided sample data.
Best regular expression to look for is
Code:
|" \n "

Because the line feed is coming right after "My_first name" field and there are more fields after "My_other" Smilie
# 4  
Old 08-07-2013
Please use code tags for posting data samples as well.

Check if this helps:
Code:
awk '/\|"[ ]*$/{ORS=""}!/\|"[ ]*$/{ORS=RS}1' file

# 5  
Old 08-07-2013
Even some shorter Smilie
awk '{ORS=(/\|"[ ]*$/)?"":RS}1' file
This User Gave Thanks to Jotne For This Post:
# 6  
Old 08-07-2013
Thanks that worked also would like to know how it worked meaning what ORS and all did to make it happen... any details of this command will help.

Thanks!
# 7  
Old 08-07-2013
Explanation:
Code:
awk '
        # Match pipe | followed by double quotes " followed by zero or more occurrence of spaces [ ]* in the end $
        /\|"[ ]*$/ {
                # Set ORS (Output Record Separator - newline by default) to ""
                ORS = ""
        }
        # Same pattern used above, but using logical not !
        !/\|"[ ]*$/ {
                # Set ORS (Output Record Separator - newline by default) to RS (Record Separator - newline by default)
                ORS = RS
        }
        # 1 == true (default awk action is to print current record)
        1
' file

ORS and RS are special awk variables. Check the manual page for further reference.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting an unexpected newline in my while loop line-by-line feed

Hi, I'm trying to get a line returned as is from the below input.csv file in Bash in Linux, and somehow I get an unexpected newline in the middle of my input. Here's a sample line in input.csv $> more input.csv TEST_SYSTEM,DUMMY@GMAIL.COM|JULIA H|BROWN And here's a very basic while loop... (7 Replies)
Discussion started by: ChicagoBlues
7 Replies

2. UNIX for Beginners Questions & Answers

awk Command to add Carriage Return and Line Feed

Hello, Can someone please share a Simple AWK command to append Carriage Return & Line Feed to the end of the file, If the Carriage Return & Line Feed does not exist ! Thanks (16 Replies)
Discussion started by: rosebud123
16 Replies

3. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

4. UNIX for Dummies Questions & Answers

awk command not replacing in first line

As per requirement if column 2 is NULL then 'N' ELSE 'Y'. I have written below awk code. But it is not replacing values for first line. :confused: cat temp.txt 1|abc|3 1||4 1|11|c awk -F'|' '{if($2==""){$2="N"}else{$2="Y"} print $0 } {OFS="|"} ' < temp.txt 1 Y 3 ... (4 Replies)
Discussion started by: max_hammer
4 Replies

5. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

6. Shell Programming and Scripting

Replacing last line with awk and change the file name

Hi Guys, I am having a set of date format files files where I am performing the below set of operations in the files . I Need to replace the last line value with specific date which is a pipe delimited file. for egf1_20140101.txt aa|aus|20140101|yy bb|nz|20140101|yy . .... (19 Replies)
Discussion started by: rohit_shinez
19 Replies

7. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

8. Shell Programming and Scripting

awk remove line feed

Hi, I've this file: 1, 2, 3, 4, 5, 6, I need to remove the line feed LF every 3 row. 1,2,3, 4,5,6, Thanks in advance, Alfredo (5 Replies)
Discussion started by: alfreale
5 Replies

9. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

10. Shell Programming and Scripting

replace last form feed with line feed

Hi I have a file with lots of line feeds and form feeds (page break). Need to replace last occurrence of form feed (created by - echo "\f" ) in the file with line feed. Please advise how can i achieve this. TIA Prvn (5 Replies)
Discussion started by: prvnrk
5 Replies
Login or Register to Ask a Question