Help with cut or awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with cut or awk command
# 1  
Old 07-08-2014
Help with cut or awk command

i am trying to cut the 4th field from the file.
sample file
Code:
a1,a2,"a,3",a4,a5,a6
b1,"b,2",b3,b4,b5,b6
c1,"c,2","c,3",c4,c5,c6

i need the output in the format of
Code:
a1,a2,"a,3",a5,a6
b1,"b,2",b3,b5,b6
c1,"c,2","c,3",c5,c6

i tried using cut -d, -f1-4,6-
but i am getting the output as
Code:
a1,a2,"a,3",a5,a6
b1,"b,2",b3,b5,b6
c1,"c,2","c,c4,c5,c6

which is wrong. any other method to do this correctly?
# 2  
Old 07-08-2014
For your input file, try:-
Code:
awk -F, '{$(NF-2)=$(NF-1) FS $NF;NF-=2}1' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-08-2014
can you please explain how the command awk works?
# 4  
Old 07-08-2014
Tricky part is that commas may be present within the field (that's why the quotes)
Yoda's solution will not work for files that have more than 6 columns, or files that have commas in the last two fields. E.g. file
Code:
a1,a2,"a,3",a4,a5,a6
b1,"b,2",b3,b4,b5,"b6,b999",b10
c1,"c,2","c,3",c4,c5,c6

will become
Code:
$ awk -F, '{$(NF-2)=$(NF-1) FS $NF;NF-=2}1' OFS=, file
a1,a2,"a,3",a5,a6
b1,"b,2",b3,b4,b5,b999",b10
c1,"c,2","c,3",c5,c6

Probably not what you want.

I would suggest the following approach:
1. protect the comma within quotes by substituting it with some character
2. get rid of the 4th field
3. substitute the protect character with comma to get back the original

Try this solution:
Code:
sed  's/"\([^,]*\),\([^,]*\)"/"\1\x1C\2"/g' file  | awk -F, '{$4 = ""}1' OFS=, | sed 's/,,/,/g ; s/\x1C/,/g'

Explanation:
Code:
sed  's/"\([^,]*\),\([^,]*\)"/"\1\x1C\2"/g' file  # replace commas within quotes with \x1C (non-ascii character unlikely to be in your file)
awk -F, '{$4 = ""}1' OFS=,                        # strip the fourth field -- now it is safe to split them by commas
sed 's/,,/,/g ; s/\x1C/,/g'                       # get rid of double commas and replace \x1C back to comma for quoted commas

# 5  
Old 07-08-2014
It cuts fields from right to left.
Yet hard-coded to the 3rd field from right.
And not all awk versions empty the fields by modifying NF.
I have put a more general awk solution to your previous post that can be easily adapted.

Last edited by MadeInGermany; 07-09-2014 at 03:33 PM..
# 6  
Old 07-09-2014
Below code will work to remove any field > 2. You need to provide the number of fields and number of the field to be removed

Code:
awk 'BEGIN{FS = OFS = "\""}
  {for(i=1; i<=NF; i+=2) {
    gsub(/,/, "\n", $i)}}1' file | awk '{
 if(NR == RM_FLD) {next};
 if(NR == FLD_CNT)
   {t = $0; getline;
   printf "%s\n%s", t, $0;
   NR = 1; f = 1; next}
 if(NR == 2 && f == 1)
   {printf "%s%s%s", ORS, $0, ORS;
   next}}1' ORS=',' RM_FLD=4 FLD_CNT=6


Last edited by SriniShoo; 07-09-2014 at 02:16 AM.. Reason: clear description
# 7  
Old 07-09-2014
need to replace comma only inside the " "(double qoutes)
sample file.
Code:
a1,a2,"a,3,12",a4,a5,a6,a7,"a,8","","",a9,a10,a11,"","",a12
b1,"b,2",b3,b4,b5,b6,"b,7",b8,"","",b9,"",b11,"","",b11
c1,"c,2","c,3",c4,c5,c6,c7,c8,"","","c,9",c10,c11,"","",c12

needed output
Code:
a1,a2,"a312",a4,a5,a6,a7,"a8","","",a9,a10,a11,"","",a12
b1,"b2",b3,b4,b5,b6,"b7",b8,"","",b9,"",b11,"","",b11
c1,"c2","c3",c4,c5,c6,c7,c8,"","","c9",c10,c11,"","",c12

i am using the command sed 's/"\([^,]*\),\([^,]*\)"/"\1\x1C\2"/g'
getting the output as
Code:
a1,a2,"a312",a4,a5,a6,a7,"a8","""",a9,a10,a11,"""",a12
b1,"b2",b3,b4,b5,b6,"b7",b8,"""",b9,"",b11,"""",b11
c1,"c2","c3",c4,c5,c6,c7,c8,"""","c9",c10,c11,"""",c12


Last edited by ATWC; 07-09-2014 at 03:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use cut output as variable piped awk command

Hi, I would like use the output of my cut command as a variable in my following awk command. Here's what I've written. cut -f1 info.txt | awk -v i=xargs -F'' '{if($6 == $i) print $20}' summary.txt Where obviously the 'xargs' doesn't do what I want. How can I pass my cut result to my awk... (3 Replies)
Discussion started by: heyooo
3 Replies

2. Shell Programming and Scripting

Problem in extracting data using cut/awk command

Hi Everyone, I have a very simple problem and i am stuck in that from last 8 days. I tried many attempts, googled my query but all in vain. I have a text file named "test.txt" In that suppose i have contents like: Java: 1 Object oriented programming language 2 Concepts of Abstraction... (5 Replies)
Discussion started by: Abhijeet Anand
5 Replies

3. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

4. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

5. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

6. Shell Programming and Scripting

awk and cut command

Hi, I have to display the value 16 present in "lcpu=16" which is the output of a command # vmstat System configuration: lcpu=16 mem=4096MB I used as # hdtype=`vmstat | grep "lcpu" | awk -F "=" '{print $2}'` # echo $hdtype 16 mem But I need to display only 16.. Am doing... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

7. Shell Programming and Scripting

AWK command to cut the desired header columns

Hi Friends, I have a file1 i want to retrieve only the fields which have DEP,CITY,TRANS as headers in other file. Output: I want to give the input as DEP,CITY,TRANS column names to get the output. i used cut command .. but if i have 300 fileds it is more difficult to... (4 Replies)
Discussion started by: i150371485
4 Replies

8. Shell Programming and Scripting

a cut-command or special format pattern in awk

Hi i read data with awk, 01.07.2012 00:10 227.72 247.50 1.227 1.727 17.273 01.07.2012 00:20 237.12 221.19 2.108 2.548 17.367 01.07.2012 00:30 230.38 230.34 3.216 3.755 17.412 01.07.2012 00:40 243.18 242.91 4.662 5.172 17.328 01.07.2012 00:50 245.58 245.41 5.179 5.721 17.128... (3 Replies)
Discussion started by: IMPe
3 Replies

9. UNIX for Dummies Questions & Answers

Regarding cut or awk command

Hi, I need a help with cut/awk command. I need to get certain values from a string provided. For example: i have a config file with below mentioned details oracle="user=name"/"pass/word"@databasename. I have used a command var1=`grep -w oracle sample.cfg | cut -d"=" -f2 | cut -d"/" -f1`. ... (10 Replies)
Discussion started by: kumars2102
10 Replies

10. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies
Login or Register to Ask a Question