Delete a Column from a Delimited File in Linux

How to delete first three columns in a delimited file

@Heng: try:

awk -F"|" '{for(i=4;i<=NF;i++){printf("%s%s",$i,i==NF?"":"|")};print ""}'  Input_file

OR

awk -F"|" '{for(i=4;i<=NF;i++){printf("%s%s",$i,i==NF?"\n":"|")};}'  Input_file

you could re-direct this command's output into a file as per your need.

EDIT:

awk -F"|" 'FNR==1{++e;fi="REPORT_A1_"e;} {for(i=4;i<=NF;i++){printf("%s%s",$i,i==NF?"\n":"|") > fi}}'   Input_file1  Input_file2  Input_file3

Delete specific columns from csv file maintaining same structure on output

Better to use sed here:

sed -E 's/^(([^,]*,){2})[^,]*,/\1/' file

12,10,10 10,1
12,23 1,6,7
11 2,33,1,2
1,2,5,6

Search regex:

  • ^: Start
  • (: Start 1st capture group

    • (: Start 2nd capture group
    • [^,]*,: Match 0 or more non-comma characters followed by a comma
    • ){2}: End 2nd capture group. {2} means match 2 pairs of above match
  • ): End 1st capture group
  • [^,]*,: Match 0 or more non-comma characters followed by a comma

Replacement:

  • \1: Back-reference to captured group #1

How to delete the first column ( which is in fact row names) from a data file in linux?

@Karafka I had CSV files so I added the "," separator (you can replace with yours

cut -d"," -f2- input.csv  > output.csv

Then, I used a loop to go over all files inside the directory

# files are in the directory tmp/
for f in tmp/*
do
name=`basename $f`
echo "processing file : $name"
#kepp all column excep the first one of each csv file

cut -d"," -f2- $f > new/$name
#files using the same names are stored in directory new/
done

linux command to delete the last column of csv

It is easy to remove the fist field instead of the last. So we reverse the content, remove the first field, and then revers it again.

Here is an example for a "CSV"

rev file1 | cut -d "," -f 2- | rev

Replace the "file1" and the "," with your file name and the delimiter accordingly.

delete a column with awk or sed

This might work for you (GNU sed):

sed -i -r 's/\S+//3' file

If you want to delete the white space before the 3rd field:

sed -i -r 's/(\s+)?\S+//3' file

Delete columns from space delimited file where file header matches


awk command

awk '
NR==1{
for(i=1;i<=NF;i++)
if($i!="size")
cols[i]
}
{
for(i=1;i<=NF;i++)
if(i in cols)
printf "%s ",$i
printf "\n"
}' input > output

pretty printing

column -t -s ' ' output 

result

id  quantity  colour  shape     colour  shape      colour  shape
1 10 blue square red triangle pink circle
2 12 yellow pentagon orange rectangle purple oval

Deleting columns from text files with sed

sed wouldn't be ideal. Use cut:

cut -d ' ' --complement -f -2,4-6,10-12 file.txt

EDIT:

From additional information from the comments:

< file.txt awk '{ print $3, $7, $8, $9 }' | column -t

Results:

KL1  -7.299  41.933  48.192
G 39.541 25.078 -2.722

To overwrite your file, you'll need to use a temporary file:

< file.txt awk '{ print $3, $7, $8, $9 }' | column -t > tmpfile && mv tmpfile file.txt

How to delete first column from the file in Unix

Using sed, delete the leading pipe symbol:

sed 's/^|//' file

There's an outside chance that on some versions of sed you'd need to escape the pipe. You might be able to use the over-write mode too (though not all versions of sed support that):

sed -i .bak 's/^\|//' file


Related Topics



Leave a reply



Submit