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
  • 71,025
  • 22
  • 98
  • 142
jonathan
  • 281
  • 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
  • 700,257
  • 99
  • 619
  • 646
  • @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
  • 15,718
  • 2
  • 32
  • 41
3

The cut command may do the trick very nicely:

echo "first;second;third;forth" | cut -d';' -f2-
ztank1013
  • 6,531
  • 2
  • 20
  • 20
1
    $ v="first;second;third;fourth"
    $ echo ${v#first;}
    second;third;fourth
    $ q=${v#*;}
    $ echo $q
DigitalRoss
  • 139,415
  • 24
  • 238
  • 326
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
  • 18,350
  • 4
  • 42
  • 46