jonathan
jonathan

Reputation: 291

How would I split the following string in bash

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?

Upvotes: 1

Views: 376

Answers (5)

matchew
matchew

Reputation: 19645

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

Upvotes: 1

DigitalRoss
DigitalRoss

Reputation: 146073

    $ v="first;second;third;fourth"
    $ echo ${v#first;}
    second;third;fourth
    $ q=${v#*;}
    $ echo $q

Upvotes: 1

ztank1013
ztank1013

Reputation: 7255

The cut command may do the trick very nicely:

echo "first;second;third;forth" | cut -d';' -f2-

Upvotes: 3

CB Bailey
CB Bailey

Reputation: 791889

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" : '[^;]*;\(.*\)'

Upvotes: 3

Fritz G. Mehner
Fritz G. Mehner

Reputation: 17188

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

str="first;second;third;fourth"

echo "${str#*;}"

Upvotes: 3

Related Questions