INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

First field repeat in all records

First field repeat in all records

(OP)
Hi
I have a file that looks like this

198606010000 1139.0000 403.3000
test1 1514.0000 487.7000
1814.0000 540.0000
2114.0000 611.5000
198606270000 717.0000 0.0000
test2 1654.0000 287.0000
2727.0000 558.0000
3012.0000 610.0000
4209.0000 832.0000
4365.0000 858.0000
4683.0000 905.0000
4868.0000 941.0000
I need to repeat the number in column1 198606010000 in all the records as the first field overwriting the text test1 until i reach to the second number 198606270000 and so on so the file looks like this
198606010000 1139.0000 403.3000
198606010000 1514.0000 487.7000
198606010000 1814.0000 540.0000
198606010000 2114.0000 611.5000
198606270000 717.0000 0.0000
198606270000 1654.0000 287.0000
198606270000 2727.0000 558.0000
198606270000 3012.0000 610.0000

I can probably do this in excel but the file is huge so I was wondering if this is possible in AWK.

RE: First field repeat in all records

I tried this:

azekry1.awk

CODE

# Run:
# awk -f azekry1.awk azekry1.txt
{
  if ($1 ~ /1986/) {
    col1 = $1
  } else if ($1 ~ /test/) {
    $1=col1
  } else {
    $0 = col1 " " $0
  }
  print $0  
} 

i have the input file
azekry1.txt

CODE

198606010000 1139.0000 403.3000 
test1 1514.0000 487.7000 
1814.0000 540.0000 
2114.0000 611.5000 
198606270000 717.0000 0.0000 
test2 1654.0000 287.0000 
2727.0000 558.0000 
3012.0000 610.0000 
4209.0000 832.0000 
4365.0000 858.0000 
4683.0000 905.0000 
4868.0000 941.0000 

when I run it, the I get the output

CODE

$ awk -f azekry1.awk azekry1.tx
198606010000 1139.0000 403.3000
198606010000 1514.0000 487.7000
198606010000 1814.0000 540.0000
198606010000 2114.0000 611.5000
198606270000 717.0000 0.0000
198606270000 1654.0000 287.0000
198606270000 2727.0000 558.0000
198606270000 3012.0000 610.0000
198606270000 4209.0000 832.0000
198606270000 4365.0000 858.0000
198606270000 4683.0000 905.0000
198606270000 4868.0000 941.0000 

Is that what have you desired ?

RE: First field repeat in all records

Hi azekry,
You seem to be new in this forum, so please next time post your example data between the tags:
[code] 
... 
[/code] 

RE: First field repeat in all records

(OP)
Yes thanks a lot that is what i want only the first column does not always start with /1986/ can i change it to something like /[0-9]/ to match any number value in the first column

RE: First field repeat in all records

You need to know what is the difference between your affected first column and other first columns.
For exampple if you know, that it has on the beginning minimal 8 digits (maybe date format yyyymmdd), you could try

CODE

# Run:
# awk -f azekry1.awk azekry1.txt
{
  if ($1 ~ /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/) {
    col1 = $1
  } else if ($1 ~ /test/) {
    $1=col1
  } else {
    $0 = col1 " " $0
  }
  print $0  
} 

RE: First field repeat in all records

If I'm using gawk ( have version 3.1.7) with the switch --posix I could write the regeex above shorter

CODE

# Run:
# gawk --posix -f azekry1.awk azekry1.txt
{
  if ($1 ~ /[0-9]{12}/) {
    col1 = $1
  } else if ($1 ~ /test/) {
    $1=col1
  } else {
    $0 = col1 " " $0
  }
  print $0  
} 

RE: First field repeat in all records

(OP)
that worked for me. Thank you so much for your help

RE: First field repeat in all records

(OP)
Please I need to ask another question I have another file that looks like this with variable text in the fourth column

CODE

200004080000            5517.9888      941.1580       car_2_f
                        5544.0151      945.1580   
                        5569.9360      949.1580   
196511290000            328.0000       0.0000         TZTho           
                        4917.8857      1296.0000  
                        14100.9385     2650.0000 

I need to format it like this:

CODE

200004080000            5517.9888      941.1580       car_2_f
200004080000            5544.0151      945.1580       car_2_f
 200004080000           5569.9360      949.1580       car_2_f
196511290000            328.0000       0.0000         TZTho           
196511290000            4917.8857      1296.0000      TZTho           
196511290000            14100.9385     2650.0000      TZTho 

RE: First field repeat in all records

I tried this

CODE

# Run:
# awk -f azekry2.awk azekry2.txt
{
  if ($1 ~ /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/) {
    col1=$1
    col4=$4
  } else {
    $3=$2
    $2=$1
    $1=col1
    $4=col4
  }
  printf "%12d\t%10.4f\t%10.4f\t%s\n", $1, $2, $3, $4  
} 
and got this output

CODE

$ awk -f azekry2.awk azekry2.txt
200004080000     5517.9888        941.1580      car_2_f
200004080000     5544.0151        945.1580      car_2_f
200004080000     5569.9360        949.1580      car_2_f
196511290000      328.0000          0.0000      TZTho
196511290000     4917.8857       1296.0000      TZTho
196511290000    14100.9385       2650.0000      TZTho 

If you are sure that in the first line (..etc) are exactly 4 columns you could use in the if-statement instead of the
condition
$1 ~ /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ 
this condition
(NF==4) 

RE: First field repeat in all records

(OP)
Yes there are 4 columns for sure. But I am a little confused. I would really appreciate if you could post the full script when you have time.

RE: First field repeat in all records

CODE

# Run:
# awk -f azekry2.awk azekry2.txt
{
  if (NF==4) {
    col1=$1
    col4=$4
  } else {
    $3=$2
    $2=$1
    $1=col1
    $4=col4
  }
  printf "%12d\t%10.4f\t%10.4f\t%s\n", $1, $2, $3, $4  
} 

RE: First field repeat in all records

(OP)
Thanks so much for all your great help

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close