awk with sed to combine lines and remove specific odd # pattern from line


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk with sed to combine lines and remove specific odd # pattern from line
# 1  
awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line in the block and is unique to each. There will always be a newline seperating the blocks and that line FileName_ID is not processed only printed. It is possible that there is nothing above the FileName_ID and if this happens then it is also printed as is. The code executes but the output is unchanged and is probably not the best way. Thank you Smilie.

file
Code:
00-0000-Lname-Fname-REPEAT
xxxx_0001 xxxx_0002
111111-yyyy
xxxx_0003 xxxx_0008
111111-yyyy-0
xxxx_0009 xxxx_0006
FileName_ID

FileName_ID

Code:
desired

xxxx_0002 00-0000-Lname-Fname-REPEAT
xxxx_0008 111111-yyyy
xxxx_0006 111111-yyyy-0
FileName_ID

FileName_ID

awk
Code:
awk 'NR%2{printf "%s ",$0;next;}1' file | sed 's/xxxx_[0-9][0-9][0-9][13579]//g'

# 2  
Hello cmccabe,

Could you please try following.

Code:
awk '
FNR==NR{
  if($0 ~ /^xxxx_[0-9]+/){
      for(i=1;i<=NF;i++){
         val=$i
         sub(/.*_/,"",val)
         if(val%2==0){
             array[++count]=$i
         }
      }
  }
  next
}
!NF || (!/^xxxx_[0-9]+/ && !/[0-9]+/){
  print
  next
}
!/^xxxx_[0-9]+/{
  print array[++count2],$0
}'   Input_file  Input_file

Output will be as follows.

Code:
xxxx_0002 00-0000-Lname-Fname-REPEAT
xxxx_0008 111111-yyyy
xxxx_0006 111111-yyyy-0
FileName_ID

FileName_ID


EDIT: Above solution will take care of only 1 EVEN id adding per line, lets say you may have multiple even ids which need to be added and printed in that case try following.

Code:
awk '
FNR==NR{
  if($0 ~ /^xxxx_[0-9]+/){
      for(i=1;i<=NF;i++){
         val=$i
         sub(/.*_/,"",val)
         if(val%2==0){
             if(FNR!=prev){
                 count++
                 prev=FNR
             }
             array[count]=(array[count]?array[count] OFS:"")$i
         }
      }
  }
  prev=FNR
  next
}
!NF || (!/^xxxx_[0-9]+/ && !/[0-9]+/){
  print
  next
}
!/^xxxx_[0-9]+/{
  print array[++count2],$0
}'   Input_file  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 04-11-2019 at 03:26 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Hello cmccabe,

My previous solution works with reading Input_file 2 times, try following with reading Input_file single time only.
Code:
awk '!NF || (!/^xxxx_[0-9]+/ && !/^[0-9]+/){
  print
  next
}
!/^xxxx_[0-9]+/{
  line=$0
  next
}
{
  for(i=1;i<=NF;i++){
     val=$i
     sub(/.*_/,"",val)
     if(val%2==0){
         value=(value?value OFS:"")$i
     }
  }
  print value,line
  line=val=value=""
}
END{
  if(line){
     print line
  }
}'   Input_file

Output will be as follows.
Code:
xxxx_0002 00-0000-Lname-Fname-REPEAT
xxxx_0008 111111-yyyy
xxxx_0006 111111-yyyy-0
FileName_ID

FileName_ID

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Another option to try:
Code:
awk '/xxxx/{print $2, p} {p=A[NR]=$0} END{for(i=2; i>=0; i--) print A[NR-i]}' file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Code:
$ awk ' /FileName_ID/ || /^$/ { print; next} { a=$0; getline; print $NF, a } ' file
xxxx_0002 00-0000-Lname-Fname-REPEAT
xxxx_0008 111111-yyyy
xxxx_0006 111111-yyyy-0
FileName_ID

FileName_ID

This User Gave Thanks to anbu23 For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #118
Difficulty: Easy
A Unix-like OS is the one that works like Unix systems, however, Unix-like system do not necessarily conform to the Single UNIX Specification (SUS) or POSIX (Portable Operating System Interface) standards.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to remove next 15 lines after pattern in Solaris

Team, I am trying to use sed to delete 15 lines, after pattern patch, which includes the pattern as well in Solaris. I used the below command, as we do it Linux, but it's not working as expected in Solaris. I am getting the error as "garbled".sed '/\/table/,+15d' status.html sed: command... (8 Replies)
Discussion started by: Nagaraj R
8 Replies

2. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

5. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

6. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

7. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

8. Shell Programming and Scripting

NAWK to remove lines that matches a specific pattern

Hi, I have requirement that I need to split my input file into two files based on a search pattern "abc" For eg. my input file has below content abc defgh zyx I need file 1 with abc and file2 with defgh zyx I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies

9. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies

10. UNIX for Dummies Questions & Answers

SImple HELP! how to combine two lines together using sed or awk..

hi..im new to UNIX... ok i have this information in the normal shell... there are 2 lines display like this: h@hotmail.com k@hotmail.com i want it to display like this with a space betweem them h@hotmail.com k@hotmail.com the information is stored in a text file.... anyone... (10 Replies)
Discussion started by: forevercalz
10 Replies

Featured Tech Videos