LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2018, 10:38 AM   #1
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Rep: Reputation: 0
Help with array in KSH


Hi Team - Below is my file

VA1|20|20|40
VA2|30|45|50
VA3|45|50|50

expected output

VA1=20,VA2=30,VA3=50

wanted to acheive this with array in KSH as i am writing script in KSH

This is what I did

while IFS="|" read -r A B C D
do
echo "$A=$C,"
done

but getting output like this
VA1=20,
VA2=30,
VA3=50,

If I use paste command last variable will have "," at the end - so want to have array used
can you please guide/assit
 
Old 08-13-2018, 10:51 AM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Code:
echo -n
will suppress the default newline.

Ideas for knowing when to print the comma:
  1. Determine how many lines there are in your input, then increment a counter and only print the comma when it's not the last line.
  2. Print the first line with no comma, then precede each subsequent line with a comma.
Not sure how using an array will help, except that's one way to manage idea #1. Another is to just
Code:
nbr_lines = $(wc -l <inputfile>)
to populate a variable with the number of lines to be processed.
 
Old 08-13-2018, 11:59 AM   #3
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
Thanks for the help
Since am new bee - can you give me some sample code I will try to enhance

or if we can write with array that will be a great help.
 
Old 08-13-2018, 12:13 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Arrow

Code:
while IFS="|" read -r A B C D
cnt=0
do
cnt++
if $cnt>1
  echo -e ","
fi
  echo -e "$A=$C"
done
Untested. Play with it.
 
Old 08-13-2018, 04:43 PM   #5
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
According to the title, I thought you talked about things like :

Code:
foo[2]=bar
To do what you want I will go with Awk, but Ksh can do the job !
 
Old 08-14-2018, 09:39 AM   #6
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Original Poster
Rep: Reputation: 0
all I want is pull the contents from the file and make them into 1 variable
Though this is easily achievable from ARRAY

So is req. some assistance from experts
 
Old 08-14-2018, 03:26 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
You can post-format with paste as follows
Code:
while IFS="|" read -r A B C D
do
  echo "$A=$C"
done < file | paste -sd, -
With shell builtins
Code:
sep=""
while IFS="|" read -r A B C D
do
  printf "${sep}%s" "$A=$C"
  sep=","
done < file
echo ""
 
1 members found this post helpful.
Old 08-14-2018, 04:22 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Arrays are often not useful in shell.
The following makes some use of an indexed array:
Code:
i=0
while IFS="|" read -r A B C D
do
  ARR[i++]=$C
done
echo "${ARR[@]}"
 
Old 08-14-2018, 11:51 PM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by MadeInGermany View Post
With shell builtins
Code:
sep=""
while IFS="|" read -r A B C D
do
  printf "${sep}%s" "$A=$C"
  sep=","
done < file
echo ""
nice trick with the commas!
 
Old 08-15-2018, 12:56 AM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by MadeInGermany View Post
Code:
sep=""
while IFS="|" read -r A B C D
do
  printf "${sep}%s" "$A=$C"
  sep=","
done < file
echo ""
Slick!
"Counter? We don't need no stinkin' counter!"
 
Old 08-15-2018, 04:22 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The following artwork can deepen your shell knowledge
Code:
myread(){
  IFS="|" read -r A B C D
}
if myread
then
  while
    out="$A=$C"
    myread
  do
    printf "%s," "$out"
  done
  echo "$out"
fi < file
Notes:
  • if-then-fi is a code block that can be redirected
  • There can be more than one statement between while-do
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ksh shell script array question Cpare Programming 1 06-19-2015 11:17 AM
[SOLVED] ksh unset on first element of associative array unsets the whole array luvshines Programming 4 05-29-2015 07:27 AM
Help with Array printing in KSH on RHEL maddyfreaks Linux - Newbie 1 03-08-2014 07:54 PM
problem to initialize ksh array when first element includes hyphen gdan2000 Programming 5 07-20-2011 04:06 AM
[SOLVED] How can I list string indexes from an array in ksh ? johnsee Programming 4 01-24-2011 04:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:46 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration