How to remove mth and nth column from a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove mth and nth column from a file?
# 1  
Old 08-06-2013
How to remove mth and nth column from a file?

Hi,

i need to remove mth and nth column from a csv file. here m and n is not a specific number. it is a variable

ex.

m=2
n=5

now i need to remove the 2nd and 5th line.. Please help how to do that.

Thanks!!!
# 2  
Old 08-06-2013
Code:
awk '{$a="";$b=""} 1' a=$m b=$n

These 2 Users Gave Thanks to Jotne For This Post:
# 3  
Old 08-06-2013
Quote:
Originally Posted by zaq1xsw2
i need to remove mth and nth column
Quote:
Originally Posted by Jotne
Code:
awk '{$a="";$b=""} 1' a=$m b=$n

Assigning an empty string does not remove a column. The number of fields in the record will remain unchanged.

Regards,
Alister
# 4  
Old 08-06-2013
Thanks a lot Jotne
But I guess we should give file name too after the command as follows. Where we have file named df_test_removing_column in which we want to remove
3rd and 4th column.



Code:
$ m=3
$ n=4
$ awk '{$a="";$b=""} 1' a=$m b=$n df_test_removing_column

Output should be as follows. It is just an exmaple for df (Disk space of AIX server.)


Code:
Filesystem 512-blocks   Iused %Iused Mounted on
/dev/hd4 262144   5812 9% /
/dev/hd2 9175040   79431 7% /usr
/dev/hd9var 3014656   3968 2% /var
/dev/hd3 2097152   113 1% /tmp
/proc -   - - /proc
/dev/hd10opt 1048576   1741 2% /opt
/dev/lv_home 720896   853 1% /home
/dev/lv_hd 524288   55 1% /opt/hd



Thanks,
R. Singh
# 5  
Old 08-06-2013
aliester is correct.
This can be cleaned by
Code:
awk '{$a=$b="";gsub(FS "+",FS)} 1' a=$m b=$n df_test_removing_column

This User Gave Thanks to Jotne For This Post:
# 6  
Old 08-06-2013
Quote:
Originally Posted by Jotne
Code:
awk '{$a="";$b=""} 1' a=$m b=$n

That is nice and quick.. Thanks.. i am using the below command on my csv file
Code:
awk -F "," 'OFS="," {$a="";$b=""} 1' a=$m b=$n output.csv

the output is like below...
Code:
AED03852181,AED03852181,AED03852181,04/08/2013,04/08/2013,1239,,3,3,3,3,,3
AED09020382,AED09020382,AED09020382,04/08/2013,04/08/2013,1239,,3,3,3,3,,3

you can see that 7th and 12th column is set to BLANK as m=7 and n=12. can you modify the above command to remove the blank column. Actually i dont want to replace ",," with a ",". then it will create problem where actually there is blank field in the original file ..

Last edited by Franklin52; 08-06-2013 at 05:16 AM.. Reason: Please use code tags
# 7  
Old 08-06-2013
This should do it
Code:
awk -F, '{$a=$b="";gsub(FS "+",FS)} 1' a=$m b=$n OFS=, df_test_removing_column

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking nth column and putting its value in n+1 column using awk

Hello Members, Need your expert opinion how to tackle below. I have an input file that looks like below: USS|AWCC|AFGAW|93|70 USSAA|Roshan TDCA|AFGTD|93|72,79 ALB|Vodafone|ALBVF|355|69 ALGEE|Wataniya (Nedjma)|DZAWT|213|50,550 I like output file in below format: ... (7 Replies)
Discussion started by: umarsatti
7 Replies

2. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

3. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

4. Shell Programming and Scripting

Break Column nth in a CSV file into two

Hi Guys, Need help with logic to break Column nth in a CSV file into two for e.g Refer below the second column as the nth column "abcd","","type/beta-version" need output in a following format "abcd","/place/asia/india/mumbai","/product/sw/tomcat","type/beta-version" ... (5 Replies)
Discussion started by: awk-admirer
5 Replies

5. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

6. Shell Programming and Scripting

Get the nth word of mth line in a file

Hi.. May be a simple question but I just began to write unix scripts a week ago, for sorting some huge amount of experiment data, so I got no common sense about unix scripting and really need your helps... The situation is, I want to read the nth word of mth line in a file, and then store it... (3 Replies)
Discussion started by: freezelty
3 Replies

7. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

8. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies
Login or Register to Ask a Question