Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8353635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:19:16+00:00 2026-06-09T09:19:16+00:00

This is really self explanatory. I’m working in a bash shell and I’m really

  • 0

This is really self explanatory. I’m working in a bash shell and I’m really new to shell scripting. I’ve found a lot of information about using tr and sed but all the examples I have found so far are removing delimiters and new lines. I really want to do the opposite of that. I want to be able to separate based on a blank space. I have a string like “abcd efgh” and I need it to be “abcd” “efgh” (all without quotes, just to show groupings).

I’m sure this is much simpler than I’m making it, but I’m very confused.

Updated Question:

I have a column of PIDs that I have put into an array, but each element of the array has both the pids in the column.

Column:

1234
5678

when I print out the entire array, all the different columns have been added so I have all the values, but when I print out a single element of my array I get something like:

1234 5678

which is not what I want.
I need to have an element for 1234 and a separate one for 5678.

This is my code so far:

!/bin/bash
echo "Enter the File Name"
read ips

index=0
IFS=' '
while read myaddr myname; do
    myips[$index]="$myaddr"
    names[$index]="$myname"
    index=$(($index+1))
done < $ips

echo "my IPs are: ${myips[*]}"
echo "the corresponding names are: ${names[*]}"
echo "Total IPs in the file: ${index}"

ind=0
for i in "$myips[@]}"
do
    echo $i
    pids=( $(jps | awk '{print $1}') )

    for pid in "${pids[@]}"; do
        echo $pid
    done

    echo "my PIDs are: ${pids}"

    for j in "${pids[@]}"
    do
        mypids[$ind]="$j"
        ind=$(($ind+1))
    done
done
echo "${mypids[*]}"
echo "The 3rd PID is: ${mypids[2]}"

SAMPLE OUTPUT:

Total IPs in the file: 6

xxx.xxx.xxx.xxx
5504
1268
1
xxx.xxx.xxx.xxx
5504
4352
1

xxx.xxx.xxx.xxx
5504
4340
1

5504
1268 5504
4352 5504
4340

The 3rd pid is: 5504
4340

I need each pid to be separate, so that each element of the array, is a single pid. So for instance, the line “The 3rd pid is: ” needs to look something like

The 3rd pid is: 5504

and the 4th element would be 4340

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:19:17+00:00Added an answer on June 9, 2026 at 9:19 am

    Try cut:

    $ echo "abcd efgh" | cut -d" " -f1
    abcd
    
    $ echo "abcd efgh" | cut -d" " -f2
    efgh
    

    Alternatively, if at some point you want to do something more complex, do look into awk as well:

    $ echo "abcd efgh" | awk '{print $1}'
    abcd
    
    $ echo "abcd efgh" | awk '{print $2}'
    efgh
    

    To address your updated question:

    I have a column of PIDs that I have put into an array, but each element of the array has both the pids in the column.

    If you want to load a column of data into an array, you could do something like this:

    $ pgrep sshd  # example command. Get pid of all sshd processes
    795
    32046
    32225
    
    $ A=(`pgrep sshd`) # store output of command in array A
    
    $ echo ${A[0]}  # print first value
    795
    
    $ echo ${A[1]}  # print second value
    32046
    

    To address the example code you posted, the reason for your problem is that you’ve change $IFS to a space (IFS=' ') which means that your columns which are separated by newlines are no longer being split.

    Consider this example:

    $ A=(`pgrep sshd`)
    $ echo ${A[0]}  # works as expected
    795
    
    $ IFS=' '       # change IFS to space only
    $ A=(`pgrep sshd`)  
    $ echo ${A[0]}  # newlines no longer used as separator
    795
    32046
    32225
    

    To avoid this problem, a common approach is to always backup the original IFS and replace it once you’ve done using the updated value. E.g.

    # backup original IFS
    OLDIFS=$IFS
    
    IFS=' '
    # .. do stuff ...
    
    # restore after use
    IFS=$OLDIFS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a self-taught, newbie programmer, and I feel like this is a really
I really hate working with IComparer - to this day, after years of working
This is self-explanatory: while (...) { var string='something that changes for each ajax request.';
A pretty self-explanatory question really, just wondering why empty tags in Ant build.xml files
This really seems like a bug to me, but perhaps some databinding gurus can
This really shouldn't be hard, I just can't figure out how to do it.
This really puzzled for hours, I searched all over the internet, but got no
So this really put headache on my head and I finally gave up and
hey guys having this really simple problem but cant seem to figure out have
I'm having this really mind breaking problem. I created a Localizable.strings file in XCode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.