Deleting columns passed as arguments to the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting columns passed as arguments to the script
# 1  
Old 03-29-2012
Bug Deleting columns passed as arguments to the script

Hi all,

I am trying to delete columns in a file using a script. The columns that need to be deleted are passed as arguments to the script.

The script should look like this
Code:
> delete_columns.sh <file_name.txt> <column_numbers_to_be_deleted>

The contents of the file_name.txt will be like
Code:
abcd
cdef
opqr

On running the script with the following arguments
Code:
> delete_columns.sh file_name.txt 4 2

the output should look like
Code:
ac
ce
oq

I wrote the following script which works with as mentioned

Code:
# This for loop removes the first argument i.e., file_name and keeps the remaining arguments in the shell variable columns_num_list
# So in the above example if arguments passed are file_name.txt 4 2
# the variable content after for loop will be columns_num_list="2,4"

for (( i = 2; i <= $#; i++ ))
do

	if [ $i -eq 2 ] 
	then 
		columns_num_list="${!i}"
	else
		columns_num_list="${!i},"$columns_num_list
	fi
done

# This command deletes the columns mentioned by columns_num_list variable
# This translates to cut --complement -c2,4 file_name.txt > file_with_columns_removed.txt

cut --complement -c$columns_num_list  $1 > file_with_columns_removed.txt

The issue is that the person who is using this script doesn't have
Code:
 --complement

option to cut command. So this script is not useful.

I have tried to explore other options like awk and couldn't actually get much headway.

Please help me in resolving this issue.
Also I am at a very beginner level in bash scripting and trying to learn. So any suggestions to improve the code that I wrote(with --complement option) would be appreciated.

P.S. As an initial try using awk, I tried to split the input file into columns using nothing as field seperator like
Code:
awk -F=""

but the version of awk that I have isn't interpreting it correctly.(I know it is pretty much screwed up if I have old versions of commands without features Smilie . But what to do. Our IT team isn't willing to update to latest linux Smilie )

Thanks a lot
Regards
VNR
# 2  
Old 03-29-2012
you can specify column to print rather than column to delete and make code more simpler:

Code:
awk -F"" '{print $1$3}'  infile

# 3  
Old 03-29-2012
Quote:
Originally Posted by 47shailesh
you can specify column to print rather than column to delete and make code more simpler:

Code:
awk -F"" '{print $1$3}'  infile

Yes, but this doesn't allow the columns to be specified dynamically. Something like the code below (provided your awk supports the -v option) will work:

Code:
awk -F "" -v collist="$columns_num_list" '
    BEGIN { OFS=""; ncol = split( collist, cols, "," ); }
    {
        for( i = 1; i <= ncol; i++ )
            $(cols[i]) = "";
        print;
    }
'  file_name.txt >new-file


Last edited by agama; 03-29-2012 at 11:40 PM.. Reason: formatting
This User Gave Thanks to agama For This Post:
# 4  
Old 04-02-2012
That worked. Thanks a lot.

Quote:
Originally Posted by agama
Yes, but this doesn't allow the columns to be specified dynamically. Something like the code below (provided your awk supports the -v option) will work:

Code:
awk -F "" -v collist="$columns_num_list" '
    BEGIN { OFS=""; ncol = split( collist, cols, "," ); }
    {
        for( i = 1; i <= ncol; i++ )
            $(cols[i]) = "";
        print;
    }
'  file_name.txt >new-file

# 5  
Old 04-02-2012
Code:
[root@dist unix]# cat file
abcd
cdef
opqr
[root@dist unix]# bash dele.sh 1 3
bd
df
pr
[root@dist unix]# bash dele.sh 2 4
ac
ce
oq
[root@dist unix]# cat dele.sh 
#!/bin/bash
while read i
do
     
     A=($(echo "${i}" | awk -F "" '{print $1,$2,$3,$4}'))
     a=$(((($(($1%2))==0))?0:1))
     b=$(((($(($2%2))==0))?2:3))
     echo "${A[$a]}${A[$b]}"
done <file
 
But this scipt  not comprehensive.

# 6  
Old 04-02-2012
How about cut?

Code:
cut -c1,3 filename

With cut you can also specify a range, so to get characters 1 to 5, then 7 to 15 and 19 to 84, you could specify:-
Code:
cut -c1-5,7-15,19-84 filename


I hope that this helps.

Robin
Liverpool/Blackburn,
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can multiple arguments be passed to shell script?

My requirement is that I want to pass similar argument to a shell script and process it in the script. Something like below: myScript.sh -c COMPONENT1 -c COMPONENT2 -a APPNote: -c option can be specified multiple times and -a is optional parameter I know this can be achieved using... (2 Replies)
Discussion started by: rajdeep_paul
2 Replies

2. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

3. Shell Programming and Scripting

deleting columns with NAs

I want to be able to delete columns whose data have more than 10 percent of NAs. x1 x2 x3 x4 1 1 1 1 2 NA 2 2 1 2 1 NA 1 2 1 NA NA 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA 1 2 1 NA so in this case i will delete x4. lets say there are 100 tables with... (1 Reply)
Discussion started by: johnkim0806
1 Replies

4. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

5. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

6. UNIX for Advanced & Expert Users

Function not called when no arguments is passed

Hi Guys, I am trying to pass arguments to the script i am wrinting. When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit. Currently, when no arguments is passed, it is not getting exited but goes on assuming those... (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Red Hat

how can i know what arguments are passed to a pre install rpm script?

hi, i have an rpm, and i am looking at the presinstall script. i can see it takes in an argument, but what i do not know is how this argument is passed to the script? is there something that calls the preinstall script? i thought the preinstall script was the first thing executed. thanks (2 Replies)
Discussion started by: JamesByars
2 Replies

8. Shell Programming and Scripting

Check if passed arguments is users

i want to check passed arguments one by one and if it is user print home director of that user (3 Replies)
Discussion started by: testman84
3 Replies

9. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

10. Shell Programming and Scripting

count no of arguments passed to a function

hi i have a function abc { //from this function i am passing args to antoher function like def a b c j k l } now i want to count the no of args coming to def() function and iterate over those values is there any way to do this one please help (2 Replies)
Discussion started by: satish@123
2 Replies
Login or Register to Ask a Question