Hot answers tagged

4

paste <(cut -f 1-8 file) <(cut -f9- file | tr -d '\t')


3

Assuming a few lines of tably separated values, generated thusly: % perl -E 'say join "\t", 1..8 for 1..3' The various columns can then be dealt with as necessary via the appropriate flags and variables and functions available in Perl. % perl -E 'say join "\t", 1..8 for 1..3' \ | perl -pale '$_=join "\t", @F[0..3], join "", @F[4..7] if $. > 1' 1 2 ...


2

awk awk ' /Cat/ { if (NR>1) print "" printf "%s", $0 next } {printf ",%s", $0} END {print ""} ' file Another version that heavily relies on awk variables: (added before I read your comment about "Cat" needing to be a case-insensitive regex) awk 'BEGIN {RS="Cat"; FS="\n"; OFS=","} NR>1 {$1=RS; NF--; print}' ...


2

With gnu split you could use the --filter option: split --numeric-suffixes=0001 -l 80 -a 4 \ --filter='sed -n -e "1,40w $FILE" -e "41,80w ${FILE/X/Y}"' infile X_ This will split the file into 80-lines pieces, piping the content of each piece to sed which writes the first 40 lines to $FILE (the piece name, in this case split replaces it with X_???? - see ...


2

One way is to use split and rename the files afterwards. But the simplest is probably to call awk. You can use the > redirection operator to write to a file instead of standard output. The variable NR contains the current line number. Awk's redirection automatically takes care of opening files. You should close files explicitly if you use a lot of ...


2

Python alternative $ cat file | python -c "import sys for line in sys.stdin: l=line.rstrip('\r\n').split('\t'); print('\t'.join(l[:9]) + ''.join(l[9:])) " sed alternative s/(([^\t]*\t){8})/\1\n/ h s/[^\n]*\n// s/\t//g G s/([^\n]*)\n([^\n]*)\n.*/\2\1/ Usage example: $ sed -r "s/(([^\t]*\t){8})/\1\n/;h;s/[^\n]*\n//;s/\t//g;G;s/([^\n]*)\n([^\n]*)\n.*/\2\...


2

Yep, many ways. I have tested the following two on a file created by: perl -le 'next if $.==1; for(1..20){print join "\t",1..20 }' > file That's a file with 20 lines and 20 tab-separated columns. Perl perl -F'\t' -ale '$"="\t";print "@F[0..7]",@F[8..$#F]' file Note that this joins all the fields from the 10th to the end. If you only want to join ...


1

Your script need to be reordered a little and awk do not need at all echo -e 'Count\tIP\tNSLookup' while read count line ; do echo -ne "$count\t$line\t" nslookup $line | grep name done < <(cut -d' ' -f1 test.apache.access_log | sort | uniq -c | sort -rn | head -3) And sure it can be done by awk only awk ' BEGIN{ OFS="\t" ...


1

We can build an array of "first value, sum of second value" and then after the whole file has been read in we can print out the results awk '{a[$1]+=$2} END { for (v in a) { print v,a[v]}}' datafile If you want the output sorted then the easiest way is to pipe that through sort: awk '{a[$1]+=$2} END { for (v in a) { print v,a[v]}}' datafile | sort -n


1

I believe you have put fifth line from the file1 too early in your example. If I'm right try this snippet: awk '(NR+1)%2{print $0;getline<"file2";n=split($0,a,", ");if(n>1)for(i in a)print a[i];next}1' file1 output: A Partridge in a Pear Tree Two Turtle Doves I was born the red planet I am hungry on Mars Three French Hens Four Calling Birds I love ...


1

With GNU sed: sed ':a;N;s/\n/,/;ta' file | sed 's/,Cat/\nCAT/g' or tr '\n' ',' < file | sed 's/,Cat/\nCAT/g'


1

You could do something like this with sed: sed '1{h;d;};/^Cat$/!{H;$!d;};x;s/\n/,/g;${x;/^Cat$/H;x;}' infile explained: sed '1{ # if this is the 1st line h # copy over the hold space d # and delete it } /^Cat$/!{ # if the line doesn't match Cat H # ...


1

This solution does not require to read the whole file into memory. In other words: It will work on a 1 TB file being processed on a 1 GB machine as long as the full lines are less than 1 GB. perl -ne 'BEGIN { $sep = shift; } if(/^$sep$/o) { @p and print join(",", @p)."\n"; @p = (); } chomp; push @p, $_; END { print join(",", $...


1

Try: awk -F\| '{print>$1}' file1 This writes each line to a file named after the first column. How it works: -F\| sets the field separator to |. print>$1 prints the current line to a file whose name is the first field.


1

awk '$3 ~ /^03/ {print $2 " " $3}' logfile should do what you want; the $3 ~ /^03/ checks that the third column from the input file ($3) starts with (^) 03. The rest is the awk pattern matching syntax (~ /pattern/). Note that this also saves the overhead of using grep as well (1 loop rather than 2 in programming terms).



Only top voted, non community-wiki answers of a minimum length are eligible