How do i find the first number in each line and insert dummy string into the missing columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i find the first number in each line and insert dummy string into the missing columns?
# 1  
Old 11-01-2011
How do i find the first number in each line and insert dummy string into the missing columns?

Hi,

I have one input file with the following content:

MY_inpfile.txt
Code:
Aname1 Cname1 Cname2 1808 5 
Aname2 Cname1 1802 47 
Bname1 ? 1819 22 
Bname2 Cname1 1784 11 
Bname3 1817 9 
Zname1 Cname1 1805 59 
Zname2 Cname1 Cname2 Cname3 1797 27

Every line in my input file have a 4 digit column (1808, 1802, 1819,....,1797) but in different position!

I would like to generate the following output file (MY_outputfile.txt):

MY_outputfile.txt
Code:
Aname1 Cname1  Cname2   DUMMYSTR  1808 5 
Aname2 Cname1  DUMMYSTR DUMMYSTR 1802 47 
Bname1 ?  DUMMYSTR DUMMYSTR   1819 22 
Bname2 Cname1  DUMMYSTR DUMMYSTR  1784 11 
Bname3 DUMMYSTR DUMMYSTR DUMMYSTR 1817 9 
Cname1 Cname1   DUMMYSTR DUMMYSTR 1805 59 
Cname2 Cname1  Cname2   Cname3  1797 27

In the output file the first 4 digit column (1808, 1802, 1819,....,1797) should be in the same position!

How do I find the first number column in each line and then insert a DUMMYSTR for each missing columns before the first number column and create output file?

I would like to use awk to achieve this.

Thanks
Szaffy


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 11-01-2011 at 11:16 AM.. Reason: Please use code tags, thank you
# 2  
Old 11-01-2011
Is the numeric 4 column in the output always going to be column 5, or is it variable (whatever the maximum num4 column was in the input)?
# 3  
Old 11-01-2011
Try this...
Code:
awk '
  NR==FNR{a[$0]=NF;x<NF?x=NF:NULL;next}
  END{
    for(i in a){
      if(a[i]==x){ print i; continue }
      split(i,arr," "); s=length(arr);
      num2=arr[s];num1=arr[s-1]
      for(j=s-1;j<=x;j++){ arr[j]="DUMMYSTR" }
      arr[x-1]=num1; arr[x]=num2
      for(j=1;j<=length(arr);j++){ printf arr[j]" " }
      printf "\n"
    }
  }' input_file | sort

--ahamed

Last edited by ahamed101; 11-01-2011 at 12:09 PM..
# 4  
Old 11-01-2011
Quote:
Originally Posted by CarloM
Is the numeric 4 column in the output always going to be column 5, or is it variable (whatever the maximum num4 column was in the input)?
Hi Carlo,

Numeric field is in variable position (column) in the input file!

First line 1808 is in column4
Second line 1802 is in column3
..
..
Last line 1797 is in column5

Regards
Szaffy

---------- Post updated at 04:02 PM ---------- Previous update was at 03:59 PM ----------

Quote:
Originally Posted by ahamed101
Try this...
Code:
awk '
  NR==FNR{a[$0]=NF;x<NF?x=NF:NULL;next}
  END{
    for(i in a){
      if(a[i]==x){ print i; continue }
      split(i,arr," "); s=length(arr);
      num2=arr[s];num1=arr[s-1]
      for(j=s-1;j<=x;j++){ arr[j]="DUMMYSTR" }
      arr[x-1]=num1; arr[x]=num2
      for(j=1;j<=length(arr);j++){ printf arr[j]" " }
      printf "\n"
    }
  }' input_file | sort

--ahamed
Hi Ahamed,

I getting the following error:

awk: Cannot read the value of arr. It is an array name.

Regards
Szaffy
# 5  
Old 11-01-2011
Working for me though... If Solaris, use nawk.
Code:
[root@bt]cat input_file
Aname1 Cname1 Cname2 1808 5
Aname2 Cname1 1802 47
Bname1 ? 1819 22
Bname2 Cname1 1784 11
Bname3 1817 9
Zname1 Cname1 1805 59
Zname2 Cname1 Cname2 Cname3 1797 27
[root@bt]awk '
>   NR==FNR{a[$0]=NF;x<NF?x=NF:NULL;next}
>   END{
>     for(i in a){
>       if(a[i]==x){ print i; continue }
>       split(i,arr," "); s=length(arr);
>       num2=arr[s];num1=arr[s-1]
>       for(j=s-1;j<=x;j++){ arr[j]="DUMMYSTR" }
>       arr[x-1]=num1; arr[x]=num2
>       for(j=1;j<=length(arr);j++){ printf arr[j]" " }
>       printf "\n"
>     }
>   }'  input_file | sort
 
Aname1 Cname1 Cname2 DUMMYSTR 1808 5
Aname2 Cname1 DUMMYSTR DUMMYSTR 1802 47
Bname1 ? DUMMYSTR DUMMYSTR 1819 22
Bname2 Cname1 DUMMYSTR DUMMYSTR 1784 11
Bname3 DUMMYSTR DUMMYSTR DUMMYSTR 1817 9
Zname1 Cname1 DUMMYSTR DUMMYSTR 1805 59
Zname2 Cname1 Cname2 Cname3 1797 27

--ahamed
# 6  
Old 11-01-2011
nm...

Last edited by CarloM; 11-01-2011 at 01:41 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the - and last id in it, indicated by the digit after the -, I am trying to use awk to print the missing line or lines in file following the pattern of the previous line. For example, in the file below the next... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Shell Programming and Scripting

Script to output a line missing a number

Ok, Lets see if I can explain this We have a script that pulls information from multiple files and outputs it, however I only need 2 Columns (of 11) from it right now I run the script like this: tkxtrn | awk '{print $5" "" "$9}' This gives me column 5 and 9, the only two I care for ... (5 Replies)
Discussion started by: shadowkraze
5 Replies

4. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

5. Shell Programming and Scripting

Find missing string with bash

If I have a file called file A with a list of filenames. How do I find all the filenames in file A that aren't contained in file B? I want to use bash scripting. (2 Replies)
Discussion started by: locoroco
2 Replies

6. UNIX for Dummies Questions & Answers

To find missing numbers from a number series

Hi, My requirement is I have an input file with a continuous series from 10000 to 99999. I have some numbers missing from those series. I want a output file which produces those missing numbers. Eg: 10002, 99999 are missing from the series then the output file should contain those... (4 Replies)
Discussion started by: rakeshbharadwaj
4 Replies

7. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

8. Shell Programming and Scripting

Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />' GREP_FOR='</JOB>' TMP_FILE=/tmp/lineArray.$$ if ]; then continue else ... (7 Replies)
Discussion started by: J-Man
7 Replies

9. AIX

How to insert dummy columns

Hi My requirement is as follows, I have a input feed coming for X as A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P; any other feed coming from Y is as ... (2 Replies)
Discussion started by: smolgara
2 Replies
Login or Register to Ask a Question