Another.Chemist
Another.Chemist

Reputation: 2559

Print all columns but first delimited by pattern, bash

Good day to all,

I was wondering how to print all columns but first, where each column is delimited by ;.

Example:

abc; def; ghi; jkl

To obtain:

def; ghi; jkl

So far I have done

awk 'BEGIN { FS = ";" } ; {for(i=2;i<NF;i++)printf "%s",$i OFS; if (NF) printf "%s",$NF; printf ORS}'

Thanks so much in advance for any clue.

Upvotes: 2

Views: 101

Answers (4)

glenn jackman
glenn jackman

Reputation: 247200

a bash function:

$ myshift() {
   local IFS=';'
   set -- $1
   shift
   echo "$*"
}

$ myshift "abc; def; ghi; jkl"
 def; ghi; jkl

Upvotes: 2

Idriss Neumann
Idriss Neumann

Reputation: 3838

Just for fun with awk :

$ echo "abc; def; ghi; jkl"|awk -F "; " '{$1=""; gsub("^ +", "", $0); print}'
def; ghi; jkl

Using sed :

$ echo "abc; def; ghi; jkl"|sed "s/^[^;]\{1,\};\ \{1,\}//g"
def; ghi; jkl

As you can see that the cut version prints a space at the beginning of the line ;)

$ echo "abc; def; ghi; jkl"|cut -d";" -f2-
 def; ghi; jkl

So you could also try something like :

$ echo "abc; def; ghi; jkl"|cut -d";" -f2-|xargs
def; ghi; jkl

Upvotes: 1

OnlineCop
OnlineCop

Reputation: 4069

If you know it will always be space-delimited:

echo "abc; def; ghi; jkl" | while read ignore line ; do echo $line ; done

The while read portion can take multiple arguments: ignore in this case will take the first segment, up to the first delimiter (meaning "abc;"), and anything not read by this is put into the rest of the arguments. In this case, it's line, so line will contain "def; ghi; jkl" which you print out.

echo "abc; def; ghi; jkl" | while read first second third ; do echo $first; echo $second; echo $third; done

will output

abc;
def;
ghi; jkl

EDIT:

If you know that it will be delimited by a different marker, you can change the IFS value:

IFS=";" ; echo "abc; def; ghi; jkl" | while read first second third ; do echo $first; echo $second; echo $third; done

produces:

abc
 def
 ghi  jkl

(notice the spaces before def and ghi).

Upvotes: 2

Sean Bright
Sean Bright

Reputation: 120714

Just use cut:

cut -f2- -d\; filename.txt

If your input is coming from stdin:

echo "abc; def; ghi; jkl" | cut -f2- -d\;

Upvotes: 5

Related Questions