Bash Cut Columns to One File and Save onto The End of Another File

Cut Column From Multiple Text Files Into New File

This will collect all of the 3rd and 4th columns concatenated together:

awk '{print $3,$4}' file1 file2 file3

If you want the 3rd and 4th columns of the second file and the third file to become the 3rd-6th columns of the output, then you need something a little fancier in bash:

paste <(awk 'BEGIN {OFS="\t"} {print $3,$4}' file1) \
<(awk 'BEGIN {OFS="\t"} {print $3,$4}' file2) \
<(awk 'BEGIN {OFS="\t"} {print $3,$4}' file3)

Alternatively, you can process each file separate and then paste them together at the end.

for f in file1 file2 file3
do
awk 'BEGIN {OFS="\t"} {print $3,$4}' $f > $f.tmp
done
paste file1.tmp file2.tmp file3.tmp

How to directly append columns of one file to another file

With bash:

#!/bin/bash

while true; do
read -r f1 <&3 || break
read -r f2 <&4 || break
echo "$f1 $f2"
done 3<fileA 4<fileB >fileC

Output to fileC:


1 1
2 2
3 3

See: https://unix.stackexchange.com/a/26604/74329

Write specific columns of files into another files, Who can give me a more concise solution?

Assuming there's no whitespace in the column 1 data, in increasing length:

sed -i "1i$(awk 'NR > 1 {print $1}' file1 | paste -sd '|')" file2

or

ed file2 <<END
1i
$(awk 'NR > 1 {print $1}' file1 | paste -sd '|')
.
wq
END

or

{ awk 'NR > 1 {print $1}' file1 | paste -sd '|'; cat file2; } | sponge file2

or

mapfile -t lines < <(tail -n +2 file1)
col1=( "${lines[@]%%[[:blank:]]*}" )
new=$(IFS='|'; echo "${col1[*]}"; cat file2)
echo "$new" > file2

Cut from column to end of line

The cut command lines in your question specify colon-separated fields and that you want the output to consist only of field 7; since there is no 7th field in your input, the result you're getting isn't what you intend.

Since the "From:" prefix appears to be identical across all lines, you can simply cut from the 7th character onward:

egrep '^From:' $file | cut -c7-

and get the result you intend.

split one file into multiple files according to columns using bash cut or awk

With awk:

awk -F '[\t;]' '{for(i=1; i<=NF; i++) print $i >> "column" i ".txt"}' file

Use tab and semicolon as field separator. NF contains the number of last column in the current row. $i contains content of current column and i number of current column.

This creates 11 files. column11.txt contains:


k
p
k
k

How to use paste to append one column from file2 to file1

Use cut and paste like so:

paste file1.txt <(cut -f2 file2.txt) > out_file.txt

Your error relates to your disk being full. Delete extra files to free up some space.

How to paste columns from separate files using bash?

You were on track with paste(1):

$ paste -d , date1.csv date2.csv 
Bob,2013-06-03T17:18:07,2012-12-02T18:30:31
James,2013-06-03T17:18:07,2012-12-02T18:28:37
Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05

It's a bit unclear from your question if there are leading spaces on those lines. If you want to get rid of that in the final output, you can use cut(1) to snip it off before pasting:

 $ cut -c 2- date2.csv | paste -d , date1.csv -
Bob,2013-06-03T17:18:07,2012-12-02T18:30:31
James,2013-06-03T17:18:07,2012-12-02T18:28:37
Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05


Related Topics



Leave a reply



Submit