6 Replies - 2390 Views - Last Post: 25 August 2012 - 12:18 AM Rate Topic: -----

#1 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

[BASH] Split string with space

Posted 23 August 2012 - 03:30 PM

Ok, I have a script which needs to output the VLC ID. However, despite my own research I am struggle with splitting the resultant string so that i can get just the ID and not the process ID refference (PID)

Here is what I have which currently just outputs PID and not the actually ID at all:

VLCID=`ps -C vlc | awk '{print $1}'`

OIFS=$IFS
IFS='\ '
ARR=$VLCID

fox x in "$ARR";
do
echo "$x"
done

IFS=$OIFS


It's been a while since I have done any bash coding and even before then i wasn't using it particularly in depth.

This post has been edited by TechnoBear: 23 August 2012 - 03:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [BASH] Split string with space

#2 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: [BASH] Split string with space

Posted 23 August 2012 - 07:42 PM

What do you mean by ID but not PID?
Was This Post Helpful? 1
  • +
  • -

#3 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

Re: [BASH] Split string with space

Posted 24 August 2012 - 08:18 AM

when you run:

ps -C vlc


you get an output of:

PID TTY TIME CMD
<ID> blah blah blah

i have managed to shorten it to just get the first column, so it will print out "PID <ID>", now i need it to just give me <ID> so i now need to split "PID <ID>" into "PID" and "<ID>" and just return "<ID>"
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,883
  • Joined: 10-May 07

Re: [BASH] Split string with space

Posted 24 August 2012 - 08:20 AM

Can you use cut? cut with -f (field) -d (delimiter) which would be space...
Was This Post Helpful? 1
  • +
  • -

#5 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

Re: [BASH] Split string with space

Posted 24 August 2012 - 08:26 AM

ok, nevermind. The problem I am getting is that it won't print the number at all. It's just printing PID i need to select second row column 1. I may have just proved myself to be an idiot

yeah I'm an idiot. My bad, i was doing it all wrong from the beginning. Thank you for trying to help though and now i see the confusion

for those with a similar issue i did it like this:

VLCID=`ps -C vlc | awk 'FNR == 2 {print $1}'


to select row two column one.

This post has been edited by TechnoBear: 24 August 2012 - 08:28 AM

Was This Post Helpful? 1
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,883
  • Joined: 10-May 07

Re: [BASH] Split string with space

Posted 24 August 2012 - 08:38 AM

Glad to see you got it working! THank you for sharing your solution!
Was This Post Helpful? 0
  • +
  • -

#7 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: [BASH] Split string with space

Posted 25 August 2012 - 12:18 AM

If I'm understanding the problem correctly you can also use the pidof command.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1