1

I'm using this command to get my output in csv.

awk 'ORS="," {print $2}'

I get the output in the following format:

a,b,c,name,col1,col2,col3,

Here I would like to delete 'name' and everything before 'name' and get only the column names. Note that column names can have the work 'name' too, which I donot want to delete. So I am okay with only deleting the first occurrence of name. Can this be done with awk?

I have tried:

awk 'NR >4 {print $2}' | awk '{ORS=","}'

and various combinations, none of which worked.

7
  • 2
    you can do with sed sed -i 's/.*name,//' /path/youfile
    – francois P
    Commented Jan 10, 2018 at 19:36
  • That sed command removed all occurrences of column names with 'name' in their name. Commented Jan 10, 2018 at 19:46
  • just as asked all before name & name itself :) 'Here I would like to delete 'name' and everything before 'name' ' it is a logical and :)
    – francois P
    Commented Jan 10, 2018 at 20:12
  • 2
    @francoisP, the greedy .* eats everything up to the last occurrence of name, so if the input is blah,name,othername,bleh, othername is gone too, and only bleh is left.
    – ilkkachu
    Commented Jan 10, 2018 at 20:17
  • 2
    What does the input file look like? Commented Jan 10, 2018 at 21:54

2 Answers 2

3

If a,b,c,name,col1,col2,col3, are the second fields of each line in the original, then you could do the test against name at the same time you pick out these (this still leaves the annoying final comma):

$ awk -vORS=, 'p {print $2}; $2 == "name" {p=1} ' input; echo
col1,col2,col3,

So, starting with what you had (awk 'ORS="," {print $2}'), we add a test variable p that tells if name has already been seen. We print the second field only if p has been set to a true value earlier, and set it to true if the second field happens to be name. With the tests in this order, the name column itself is not printed. We could also ignore empty lines in input by changing p {print $2} to $0 && p {print $2}, that is, make a truthy (nonempty) input line a condition for the print, along with p.

I assumed here that the original input looks like this:

x a
x b
x c
x name
x col1
x col2
x col3

Alternatively, starting from the comma-separated list a,b,c,name,somename,othername,col3,:

$ echo 'a,b,c,name,somename,othername,col3,' | 
    sed -e 's/.*,name,//' -e s'/,$//'
somename,othername,col3

Note the commas on both sides of ,name, in the pattern, they keep the greedy .* from catching the later names that end in ...name.

6
  • This almost worked. I now get ",col1,col2,col3,". I need to remove the first comma after the name Commented Jan 10, 2018 at 19:49
  • I think the extra comma at the beginning might be due to leading whitespaces/new lines? Commented Jan 10, 2018 at 19:55
  • @user2441441, mhmh, well, I can't see the original data you used to produce the comma-separated list, so it's hard to say. Empty lines would cause that, though.
    – ilkkachu
    Commented Jan 10, 2018 at 19:58
  • Thanks. Do you know how to remove the extra comma? Commented Jan 10, 2018 at 20:00
  • Can you please explain how the command works awk -vORS=, 'p {print $2}; $2 == "name" {p=1} '? Commented Jan 10, 2018 at 20:07
3

Assuming file contents as below:

$ cat myfile
a,b,c,name,col1,col2,col3,forename,surname,name5,foo,name,name6
$ 

awk solution

$ awk -F',name,' '{print substr($0,index($0,$2))}' myfile
col1,col2,col3,forename,surname,name5,foo,name,name6
$

perl solution.

$ perl -pe 's/^.*?name,//' myfile
col1,col2,col3,forename,surname,name5,foo,name,name6
$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .