refine awk command in replacing carriage return


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting refine awk command in replacing carriage return
# 1  
Old 08-04-2011
refine awk command in replacing carriage return

Hi,

need your help in below,I have 4 types of file need to be processed so that it will replace carriage return in Remarks column with <:::>
Remarks column position may varies in different types of file.

sample file:
Code:
col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|11|12|13|14|15|16
|Testing 
remarks|18|19
A_2|21|22|23|24|25|26|Testing
 
remarks|28|29

desired output:
Code:
col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|11|12|13|14|15|16|Testing:::remarks|18|19
A_2|21|22|23|24|25|26|Testing:::remarks|28|29

tukuyomi helps in this requirement with below script
Code:
awk -F\| 'FNR==1{n=NF}NF<n{l=$0;getline;$0=l":::"$0}1'

but it is not work when the carriage return happen twice (with empty row) and the carriage return is happen before a pipe <|>

Your help is much appreciate

Last edited by radoulov; 08-04-2011 at 06:58 AM.. Reason: Code tags.
# 2  
Old 08-04-2011
you may remove the empty line before using the awk command ...

so if ur input is in sample file then u may use

Code:
grep -v '^$' sample | awk -F\| 'FNR==1{n=NF}NF<n{l=$0;getline;$0=l"
:::"$0}1'
col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|11|12|13|14|15|16|Testing :::remarks|18|19
A_2|21|22|23|24|25|26|Testing:::remarks|28|29


Last edited by radoulov; 08-04-2011 at 06:58 AM.. Reason: Code tags.
# 3  
Old 08-04-2011
Thanks Shipra_31 for the script.

However, when in below situation - carriage return is occurred after "16", it is not working. In addition, it should not add <:::> How to tackle this? your help is much appreciate Smilie

sample file:
Code:
col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|11|12|13|14|15|16
|Testing 
remarks|18|19

desire output:
Code:
col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|11|12|13|14|15|16|Testing:::remarks|18|19


Last edited by radoulov; 08-04-2011 at 06:59 AM.. Reason: Code tags.
# 4  
Old 08-04-2011
Hi,

Try next 'perl' command:
Code:
$ perl -ne 'BEGIN { $" = "" } if ( $. == 1 ) { print ; next } ; s/\s*$// ; unless ( /^(?i:remarks)/ ) { push @reg, $_ ; next }  ; printf "%s\n", "@reg" . ":::" . $_ ; undef @reg' infile
col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|11|12|13|14|15|16|Testing:::remarks|18|19
A_2|21|22|23|24|25|26|Testing:::remarks|28|29

Regards,
Birei
# 5  
Old 08-04-2011
it doesn't work here.
It shows only the column header, the rest of data is chopped off.

output:
col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
# 6  
Old 08-05-2011
Very poor input format, newline has 3 meanings, after "col10" it marks a new record, after "16" it is just to be ignored and within field #8 its an imbedded symbol!

This works for your input example - see how you go with it:

Code:
awk ' {
  a[F++]=$0;
  if(F==8) gsub(/\n/,":::",a[F-1])
  else if(F<10) gsub(/\n/,"",a[F-1])
  else {
      split(a[F-1],b,"\n")
      a[9]=b[1]
      for(i=0;i<9;i++) printf a[i]"|"
      printf a[9]"\n"
      delete a
      F=0
      if(b[2]) a[F++]=b[2]
  }}' FS= RS=\| infile

# 7  
Old 08-11-2011
Thanks Chubler_XL for the code.

I haven't test out the script given. I am trying to understand the script before testing on the source file. I have included the comment on each if else statement, but i failed to figure out on the last else statement. I would need your help in explaining the script so that i can modify it as per my need as the sample input and output given is just for illustration purpose. Thanks in advance.

awk ' {
a[F++]=$0;
if(F==8) gsub(/\n/,":::",a[F-1]) #imbedded symbol
else if(F<10) gsub(/\n/,"",a[F-1]) #new record
else { #need your explanation
split(a[F-1],b,"\n")
a[9]=b[1]
for(i=0;i<9;i++) printf a[i]"|"
printf a[9]"\n"
delete a
F=0
if(b[2]) a[F++]=b[2]
}}' FS= RS=\| infile
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Losing carriage return (X0D) after running awk command

Hi Forum. I'm running the following awk command to extract the suffix value (pos 38) from the "AM00" record and append to the end of the "AM01" record. awk 'substr($0,13,4)=="AM00" {SUFFIX = substr($0,38,2)} substr($0,13,4)=="AM01" {$0 = $0 SUFFIX} 1' before.txt > after.txt Before.txt:... (2 Replies)
Discussion started by: pchang
2 Replies

3. Shell Programming and Scripting

Substitute \n with carriage return

Hello all, I've a flat file in the following format: AB\001\CDED\001\ABC\001\nEG\001\HIJF\001\EFG\001\nHI\003\HIUL\003\HIJ\003 And I want to substitute \n with the carriage return. Any help is appreciated! Regards, - Seth (8 Replies)
Discussion started by: sethmj
8 Replies

4. Shell Programming and Scripting

Awk to remove carriage return from 65th field

Hi, I have a pipe delimited file. There are around 700 columns in the file. The 65th column has carriage return which is causing read issue with our ETL process. I would like to replace the new line characters in 65th field with "nothing" i have return the following code and need help to... (7 Replies)
Discussion started by: pinnacle
7 Replies

5. Shell Programming and Scripting

Why sed command deletes last line in a file if no carriage return?

Hi I am using sed command to make SCORE=somevalue to SCORE=blank in a file. Please see the attached lastline.txt file. After executing the below command on the file, it removes the last line. cat lastline.txt | sed 's/SCORE=.*$/SCORE=/g' > newfile.txt Why does sed command remove the... (3 Replies)
Discussion started by: ashok.k
3 Replies

6. Shell Programming and Scripting

Refine awk

Hi The commnad shows the following o/p df -kh /abc/bds/ |awk {'print$5'} capacity 87% I want the o/p as 87 only Can we achieve this using a oneliner of sorts NB : Code tags not working with my google chrome , So wasn't able to place code in tags . Pls dont penalise :-) Thanks (6 Replies)
Discussion started by: ultimatix
6 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

tcl command to supply a carriage return

I have a java program that runs on a unix server that prompts the user for input and provides a default value to the user. So it does something like this: Enter source server name <source_server_name>: Enter target server name <target_server_name>: I just hit enter to take the default... (1 Reply)
Discussion started by: progkcp
1 Replies

9. Shell Programming and Scripting

Carriage return on long awk operation

hello all I have long awk function that doing manipulations on text file but when I write the out put to new text file I have carriage return between 2 print commands How can I avoid this ? Here is my awk : echo $f | awk... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Dont want carriage return

I have observed with print & echo, they produce carriage return <CR> or newline, after they display string next to them. Is there anyway to avoide these <CR> after the intended string is displayed? (3 Replies)
Discussion started by: videsh77
3 Replies
Login or Register to Ask a Question