Change text in a file (millions of columns, tab delimiter) - delete all tabs except the first, and add new columns after the final column

Here I want your files to help change the text file.

I want to change the text in the file (millions of columns, tab delimiter) - delete all tabs except the first, and add new columns for the last column.

  • file (4 columns here, but millions of columns in my real file, tab-bounded) I have:

    day1 1 3 7
    day7 2 4 8
    day3 2 5 6
    
  • the file I want is to remove the delimiter (from the second) and add three new columns (one is the same as the first, two others with the same value in the whole one column, here x and y).

    day1 137 day1 x y
    day7 248 day7 x y
    day3 256 day3 x y
    

I intend to do this with awk mixed with sed. But, I tried many different ways, I still don’t know how to do it.

could you help me? Thanks in advance.

,

+3
3

gawk -f script.awk < input > output script.awk :

{
    old=$1;
    $0 = substr($0, length($1)+1);
    gsub(/[[:space:]]*/, "", $0);
    print old, $0, old, "x", "y";
} 

live ( belisarius )

+2

[perl], :

#! /usr/bin/perl -lan

BEGIN { $, = "\t" }

$first = shift @F;
print $first, join("", @F), $first, qw/ x y /;

, .

  • $, TAB, FS awk
  • -l "\n" print, ORS awk
  • -a ( awk).

-n -p. split @F while, -n -p.

perl -ane 'print pop(@F), "\n";'

while (<>) {
  @F = split(' ');
  print pop(@F), "\n";
}

, Perl , sed -n awk:

LINE:
while (<>) {
  ...  # your program goes here
}
+2

:

sed 's/\t//2g;s/\(.*\t\)\(.*\)/&\t\1x\ty/' file
day1    137     day1    x       y
day7    248     day7    x       y
day3    256     day3    x       y

, , . :

sed 's/\t//2g;s/\(.*\)\t\(.*\)/& \1 x y/' file
day1    137 day1 x y
day7    248 day7 x y
day3    256 day3 x y
+1

All Articles