简体   繁体   中英

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.

Just use cut :

cut -f2- -d\; filename.txt

If your input is coming from stdin:

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

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

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 ).

a bash function:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM