LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-03-2010, 10:28 AM   #1
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Rep: Reputation: 15
how to create a single line of output from multiple variable lines of input


I need to create a single line of output from multiple and variable lines of input in a Linux bash shell script.

My input file looks like this:
telephonenumber: 4235551212
umcosdn: 235
umsecondaryphonenumber: 4239434871
umsecondaryphonenumber: 4239434873
umsecondaryphonenumber: 4239434874
umsecondaryphonenumber: 4239434875
telephonenumber: 4235551216
umcosdn: 231
telephonenumber: 4235551216
umcosdn: 231
umsecondaryphonenumber: 4239434876
.
.
.

where there may be any number of umsecondaryphonenumber lines; if there is not a umsecondaryphonenumber line for a telephonenumber, I don't want to write any output.

So, the output file should look like:
4235551212,235,4239434871,4239434873,4239434874,4239434875
4235551216,231,4239434876

The script I have so far is:

first_time="yes"
altcount=0
save_telephone_number=""
havealtnumber="no"
while read line
do
attr=`echo $line | cut -f1 -d":"`
value=`echo $line | cut -f2 -d":" | sed "s/^ //"`
case $attr in
telephonenumber)
if [ "$first_time" = "yes" ]
then
save_telephone_number=$value
first_time="no"
else
if [ "$havealtnumber" = "yes" ]
then
echo "$save_telephone_number,$umcosdn,howdoiprintarray"
havealtnumber="no"
fi
save_telephone_number=$value
fi
;;
umcosdn)
umcosdn=$value
;;
umsecondaryphonenumber)
altarray[$altcount]=$value
((altcount++))
havealtnumber="yes"
;;
*)
esac
done < /tmp/primary-alt.txt

My question is - how do print each of the elements of an array in one record - i.e. what do I put in place of howdoiprintarray?

Thanks in advance.
 
Old 02-03-2010, 02:28 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
First of all, please use [code][/code] tags around your code to preserve formatting (and start using tab indenting in your code if that's how it originally looked ). It took me a couple of minutes of reformatting to figure out what your script is supposed to do.

Here's what it looks like after I cleaned it up a bit. Note that I substituted $() for `` backticks, as recommended, and "elif" for "else if". I haven't changed anything else yet.
Code:
first_time="yes"
altcount=0
save_telephone_number=""
havealtnumber="no"

while read line; do
     attr=$(echo $line | cut -f1 -d":")
     value=$(echo $line | cut -f2 -d":" | sed "s/^ //")
     case $attr in
          telephonenumber)
               if [ "$first_time" = "yes" ]; then
                    save_telephone_number=$value
                    first_time="no"
               elif [ "$havealtnumber" = "yes" ]; then
                    echo "$save_telephone_number,$umcosdn,howdoiprintarray"
                    havealtnumber="no"
                    save_telephone_number=$value
               fi
          ;;
          umcosdn)
               umcosdn=$value
          ;;
          umsecondaryphonenumber)
               altarray[$altcount]=$value
               ((altcount++))
               havealtnumber="yes"
          ;;
          *)  ;;
     esac
done < /tmp/primary-alt.txt

The main problem I see is that you can't print the data you're collecting while inside the loop, because that only operates on one line at a time. What you need to do is redesign the thing so that the loop collects all the data you want to print, then print everything at once after you exit it.

Of course, the printing of the array itself is fairly straightforward.
Code:
echo ${altarray[@]}| tr " " ","
 
1 members found this post helpful.
Old 02-03-2010, 03:09 PM   #3
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Thanks!


The code was tab indented when I pasted it in; I was not aware of the code tag but will give it a try.


I have always used backticks but $() works just as well.


I used the print technique you suggested - for the section of code:

Code:
if [ "$havealtnumber" = "yes" ]
then
echo "$save_telephone_number,$umcosdn,howdoiprintarray"
havealtnumber="no"
fi
I replaced it with:

Code:
      if [ "$havealtnumber" = "yes" ]
      then
        echo "$save_telephone_number,$umcosdn,${altarray[@]}" | sed -e "s/ /,/g" | sed -e "s/,*$//g"
        havealtnumber="no"
        numberofalts=${#altarray[@]}
        altcount=0
        while [ $altcount -lt $numberofalts ]
        do
          altarray[$altcount]=""
          ((altcount++))
        done
        altcount=0
      fi
and now it works as expected. Thanks again.
 
  


Reply

Tags
array, print


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk or sed to use CSV as input and XML as template and output to a single file bridrod Linux - Newbie 6 03-13-2012 07:00 PM
have a Variable with multiple lines of data need to have it all on one line seperated xskycamefalling Programming 11 05-12-2009 04:44 AM
Storing multiple options in a single variable (binary addition?) matt_heys Programming 1 04-03-2009 03:38 PM
input single integer value, output the value as separate digits hubabuba Programming 2 03-02-2005 10:42 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM


All times are GMT -5. The time now is 09:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration