Bash: adding commas in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: adding commas in for loop
# 1  
Old 04-17-2012
Bash: adding commas in for loop

Dear Experts

I think this is possibly the easiest thing. but I am not able to solve:

I need comma to be added to end of each line echo'd. But does not want it to be added to last line.

I have a script which does some data analysis and creates a command as in below code snippet

Code:
for file in $(sort -u -k3nr data  | cut  -d, -f2| head -5 | tr -d ' ')
do
 echo "\"<grep -w $file data\"  using 1:3 title '$file' with linespoints ,\\"
done

Now output I get is

Code:
"<grep -w dir1 data" using 1:3  title 'dir1' with linespoints ,\
"<grep -w dir23 data" using 1:3  title 'dir23' with linespoints ,\
"<grep -w whatever data" using  1:3 title 'whatever' with linespoints ,\

Now I don't want to have comma and slash on last line.

I don't know how many sort command will pull out. and also don't want to have a command wc -l to calculate before hand. I have been trying echo -e "\b" without
luck ..

any other suggestions
# 2  
Old 04-17-2012
Hi,

Try this..!!
This will help you when you write all the commands which you created using a for loop to a file.

Assuming you modify your code as:

Code:
 
echo "\"<grep -w $file data\"  using 1:3 title '$file' with linespoints ,\\" >> cmd.txt

Use the below code to remove ,\ from last line

Code:
sed  '$s/,\\$//' cmd.txt

# 3  
Old 04-17-2012
Thank you Smilie . This is really good.
Usually I dont like to create a temp file. Had a bad experience

I am keeping everything in variable and want to execute the command.

Even though I said I dont want to use wc -l .. For now I am doing

nodir=$(sort -u -k3nr data | cut -d, -f2| head -5 | tr -d ' '|wc -l)

and comparing

[[ $nodir > 0 ]] && echo ",\\"
[[ $nodir == 0 ]] && echo " "
# 4  
Old 04-17-2012
Code:
for file in $(sort -u -k3nr data  | cut  -d, -f2| head -5 | tr -d ' ')
do
 echo "\"<grep -w $file data\"  using 1:3 title '$file' with linespoints ,\\"
done | sed '$s/...$//'

Code:
result=$(
  for file in $(sort -u -k3nr data  | cut  -d, -f2| head -5 | tr -d ' ')
  do
    echo "\"<grep -w $file data\"  using 1:3 title '$file' with linespoints ,\\"
  done
)

echo "${result%???}"

# 5  
Old 04-17-2012
Wow .. I knew I asked at right place ..
Thanks a ton guys ..

I like sed .. using sed method
sed '$s/...$//'
or what asterisk gave

sed '$s/,\\$//'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

adding one more column in a loop

data1 1 0.01 3 5 1 0.6 2 1 data2 2 0.02 3 5 2 0.3 2 1 data3 3 0.01 3 5 3 0.01 2 1 output 1 0.01 data1 2 0.02 data2 3 0.01 data3 3 0.01 data3 I want to print 1st, 2nd column and the filename when second column is less than 5. (3 Replies)
Discussion started by: johnkim0806
3 Replies

2. Shell Programming and Scripting

Adding Extra Commas to a CSV file

Trying in this forum. Not sure if it is permitted.... but in need of help. Please find the requirements in the below link. https://www.unix.com/unix-dummies-questions-answers/191503-add-extra-commas-csv-file-2.html#post302665179 Thanks in Advance. (1 Reply)
Discussion started by: chillblue
1 Replies

3. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

4. Shell Programming and Scripting

append file by adding commas

Hi, Iam working on Sun solaris machine.I have a file with ids in the below format. 1 2 3 4 5 I want it as: 1,2,3,4,5 I use these commands individually to get into that format: vi file /1,$s/$/,/g This adds commas in the file and then i save it. next i use this command :... (8 Replies)
Discussion started by: jyothi_wipro
8 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. UNIX for Dummies Questions & Answers

Inserting commas and replacing backslashes with commas

Hi, Newbie here. I have a file that consists of data that I want to convert to a csv file. For example: Jul 20 2008 1111 / visit home / BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLOC-1.1 VendorID/105 Jul 21 2008 22222 / add friend / BlackBerry8830/4.2.2 Profile/MIDP-2.0... (3 Replies)
Discussion started by: kangaroo
3 Replies

7. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

8. Shell Programming and Scripting

adding values with a loop

Hi there, I am checking disk spaced used on a box # df -k | grep dsk | awk {'print $3'} 2055463 20165785 18310202 32274406 I want to somehow add them up but am no quite sure how to do this in a loop. ...i.e for n in 'df -k | grep dsk | awk {'print $3}' do <some adding... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

9. Programming

Removing empty spaces and adding commas

I have a file which contains numbers as follows: 1234 9876 6789 5677 3452 9087 4562 1367 2678 7891 I need to remove the empty spaces and add commas between the numbers like: 1234,9876,6789,5677,3452, 9087,4562,1367,2678,7891 Can anyone tell me the command to do... (4 Replies)
Discussion started by: jazz
4 Replies

10. UNIX for Dummies Questions & Answers

Question on files - adding commas at the end

Hi All, I have a file. Each line suppose to contain 188 fields delimited by , (comma --> so 187 commas) but some records have less than 188 fields which needs to be filled by additional commas at the end. Ex: a file wth 5 columns and 4 commas test1,test2,test3 red1,red2... (5 Replies)
Discussion started by: jingi1234
5 Replies
Login or Register to Ask a Question