Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row to column and column to row using awk

Tags:

awk

I have two files containing as below

cat file1.txt
a b c 
1 2 3

cat file2.txt
a
b
c
1
2
3

I want file1 to be arranged as

a
b
c
1
2
3

and file2 to be arranged as

a b c
1 2 3

I would like a solution using awk one line

like image 816
user2949699 Avatar asked Nov 03 '13 11:11

user2949699


People also ask

What is awk '- F option?

For example: awk -F, ' program ' input-files. sets FS to the ' , ' character. Notice that the option uses an uppercase ' F ' instead of a lowercase ' f '. The latter option ( -f ) specifies a file containing an awk program.

Can I use cut with awk?

The awk implementation of cut uses the getopt() library function (see Processing Command-Line Options) and the join() library function (see Merging an Array into a String). The current POSIX version of cut has options to cut fields based on both bytes and characters.

What is ORS in awk?

awk Built-in Variables ORS - Output Record Separator This variable is used to set output record separator, by default a newline.

What is awk print $NF?

AWK NF in Ubuntu 20.04: The “NF” AWK variable is used to print the number of fields in all the lines of any provided file. This built-in variable iterates through all the lines of the file one by one and prints the number of fields separately for each line.


1 Answers

I'd use xargs for this:

$ xargs -n1 < file1
a
b
c
1
2
3

$ xargs -n3 < file2
a b c
1 2 3
like image 177
Chris Seymour Avatar answered Sep 28 '22 12:09

Chris Seymour