1

Suppose we have a string of the form

first;second;third;fourth

I would like to print

second;third;fourth

How would I do it?

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
jonathan
  • 291
  • 1
  • 5
  • 17

5 Answers5

3

Reading between the lines of your requirements, if you want to print everything after the first semicolon, I would use the POSIX standard expr utility.

expr "first;second;third;fourth" : '[^;]*;\(.*\)'
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • @ennuikiller: What exactly do you mean by cryptic? It was the most portable solution that I could think of at the time. – CB Bailey Oct 29 '11 at 20:19
3

Use parameter substitution (match beginning; delete shortest part):

str="first;second;third;fourth"

echo "${str#*;}"
Fritz G. Mehner
  • 16,550
  • 2
  • 34
  • 41
3

The cut command may do the trick very nicely:

echo "first;second;third;forth" | cut -d';' -f2-
ztank1013
  • 6,939
  • 2
  • 22
  • 20
1
    $ v="first;second;third;fourth"
    $ echo ${v#first;}
    second;third;fourth
    $ q=${v#*;}
    $ echo $q
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
1
echo "first;second;thrid;fourth" | awk -F";" '{print substr($0,index($0,$2))}'

A lot of these answers work, and I think cut may be the best solution, but its a slow night so I added another, print field 2 to end of the line.

Its very similar to a different question however:

Print Field 'N' to End of Line

Community
  • 1
  • 1
matchew
  • 19,195
  • 5
  • 44
  • 48