0

Hi,
i want to delete a column from a file. the file is like :
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0

if i want to remove any column the outout should be like ( suppose 4):
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0

i have used swk command, but its not giving the desired output: its giving some extra delimeters:

awk 'BEGIN{FS=OFS="|"}{$4="";gsub(FS,"")}1' file

please help

3
Contributors
12
Replies
18
Views
4 Years
Discussion Span
Last Post by Liam Huang
Featured Replies
  • hi, awk -F'|' '{ for(n=1; n<=NF; n++){ if(n!=NF)fmt="%s|"; else fmt="%s\n" if(n!=4)printf(fmt,$n) } }' UrFile Read More

  • while [ -n "$1" ] do ... shift done or for arg #in "$@" is not needed do ... done Read More

  • fmt is a user defined variable; I put the format to be used according to current field is the last field or not. NF is an awk variable, it's "The number of fields in the current input record." (so says gawk's man page) Read More

1

hi,

awk -F'|' '{
   for(n=1; n<=NF; n++){
      if(n!=NF)fmt="%s|"; else fmt="%s\n"
      if(n!=4)printf(fmt,$n)
   }
}' UrFile
0

I want to use it in a loop with taking input from the user ,
i tried to place a loop but its not working.

0

how is it not working?

please show what you want, and what you've tried.

Edited by Watael

0
     while [ "$*" != " " ]
        do
        awk -v awk_var=$1  -F'|' '{
           for(n=1; n<=NF; n++){
              if(n!=NF)fmt="%s|"; else fmt="%s\n"
              if(n!= awk_var)printf(fmt,$n)
           }
        }' new

        echo "$1 Column deleted"
        shift

     done

i used this code.. but its going to a infinite loop, i want to use command line arguments as input

Edited by nikita.

2
while [ -n "$1" ]
do
   ...
   shift
done

or

for arg #in "$@" is not needed
do
   ...
done

Edited by Watael

0
while [ -n "$1" ]
do
awk -F'|' -v awk_var=$awkvar '{
   for(n=1; n<=NF; n++){
      if(n!=NF)
         fmt="%s|";
      else
        fmt="%s\n";
      if(n!=awk_var)printf(fmt,$n)
   }
}' new > new.new

mv new.new new

echo "$1 Column deleted"
shift
((awkvar=$1-1))
done
0
awk -F'|' -v awk_var=$awkvar '{
for(n=1; n<=NF; n++){
if(n!=NF)
fmt="%s|";
else
fmt="%s\n";
if(n!=awk_var)printf(fmt,$n)
}
}' new 

can u please explain the above code

0

there is not much to explain :( the code is quite simple.

what don't you understand?

1

fmt is a user defined variable; I put the format to be used according to current field is the last field or not.
NF is an awk variable, it's "The number of fields in the current input record." (so says gawk's man page)

0

There is no need to use awk to solve this. A short but easy-to-understand solution with cut:

$ cat a 
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0
1|2|3|4|5|6|7|8|9|0

$ cut -d'|' -f4 --complement a
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0
1|2|3|5|6|7|8|9|0

Edited by Liam Huang: topy

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.