Add line number to for loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add line number to for loop?
# 1  
Old 06-17-2015
Add line number to for loop?

Code:
> cat test.sh
for t in `cat out.txt`
do
echo create directory data as "'"$t"';"
done
>


output of out.txt is as below
Code:
/oracle/SID/data1/file2_1/
/oracle/SID/data1/file1_1/
/oracle/SID/data1/file5_5/
/oracle/SID/data1/file_9/
/oracle/SID/data1/file_2/


when i run my test.sh script i get below output...
Code:
create directory data as '/oracle/SID/data1/file2_1/';
create directory data as '/oracle/SID/data1/file1_1/';
create directory data as '/oracle/SID/data1/file5_5/';
create directory data as '/oracle/SID/data1/file_9/';
create directory data as '/oracle/SID/data1/file_2/';

i want to modify my test.sh script to my output looks like below...note how the data changes to data_1, data_2 (basically line number, however many lines in out.txt)
Code:
create directory data_1 as '/oracle/SID/data1/file2_1/';
create directory data_2 as '/oracle/SID/data1/file1_1/';
create directory data_3 as '/oracle/SID/data1/file5_5/';
create directory data_4 as '/oracle/SID/data1/file_9/';
create directory data_5 as '/oracle/SID/data1/file_2/';


Last edited by vgersh99; 06-17-2015 at 04:01 PM.. Reason: code tags, please!
# 2  
Old 06-17-2015
Code:
I=1
for t in `cat out.txt`
echo "create directory data_$i  as" '"$t"' :"
i=`expr $i + 1`
done

---------- Post updated at 07:03 PM ---------- Previous update was at 07:02 PM ----------

The first line is i=1

---------- Post updated at 07:04 PM ---------- Previous update was at 07:03 PM ----------

Code:
i=1
for t in `cat out.txt`
do
echo create directory data_$i  as" '"$t"' :"
i=`expr $i + 1`
done


Last edited by Corona688; 06-17-2015 at 04:26 PM..
This User Gave Thanks to Khanaza For This Post:
# 3  
Old 06-17-2015
Thank you, that is what i was thinking too after i posted...
# 4  
Old 06-17-2015
Welcome, sorry for the multiple post, i am using mobile to post it, have a nice day
# 5  
Old 06-17-2015
Code:
awk -v q="'" '{print "create directory data_" FNR " as " q $0 q ";"}' out.txt

# 6  
Old 06-17-2015
With a recent shell:
Code:
while read t; do echo create directory data_$((++c)) as "'"$t"';"; done < out.txt

# 7  
Old 06-18-2015
Variation on RudiC's suggestion with any POSIX compliant shell:
Code:
c=0
while read t
do
  printf "%s\n" "create directory data_$((c+=1)) as '$t';"
done < out.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of words in line, while loop, search and grep

Hello, What I wish to attain is: - to read fileA line by line - search entire line as string in fileB - when found, grep the next line in fileB - then merge "searched line" and "found line" in a new file, fileC Here is my fileA: T S Eliot J L Borges L Aragon L L Aragon T S Eliot 4 0... (17 Replies)
Discussion started by: baris35
17 Replies

2. Shell Programming and Scripting

How to add line to previous line if not a number?

Hi, I am trying to compare 2 lists. However, one of these lists has to be taken from a.pdf file. When I copy the test into a .txt document there are formatting errors which I need to correct. The document is long (~10,000 lines) so I need to script the re-formatting. Currently my file looks... (9 Replies)
Discussion started by: carlr
9 Replies

3. Shell Programming and Scripting

Choose a line-number to add line

Hello guys, I'm making a script to add visudo with this script. Do you guys know if it's possible to add words to a line-number you want to. Something like this: echo "Adding words to line-number 16" >> /etc/sudoers # (options to add to line-number-16)? Thanks! (3 Replies)
Discussion started by: dannyvdberg
3 Replies

4. Shell Programming and Scripting

How to add line number using UNIX command?

Hi, I am working on Shell script and I have a .dat file in which I want to add line numbers. Please see below example file Input File: 19523479811841494432C2013052700000000 19523479811730333980A2013052700000000 19523479811417044397I2013052700000000 19523479811205895810A2013052700000000... (7 Replies)
Discussion started by: jnrohit2k
7 Replies

5. Shell Programming and Scripting

Add markup tag and sequential number after specific line

Hello, This one has me a bit stumped. I have data the looks like, M END > <PREDICTION_ACCURACY> PROBABLE > <NO_OF_PARENTS> 3 > <CLOGP> -13.373 > <SMILES> OCC(O)C(OC1OC(CO)C(OC2OC(CO)C > <MIMW> 1006.322419888 (3 Replies)
Discussion started by: LMHmedchem
3 Replies

6. Shell Programming and Scripting

add a number to the beginning of every line

hey, i would like to add a line number to the beginning like so: red blue green yellow will be: 1=>red 2=>blue 3=>green 4=>yellowplease advise thank u. (5 Replies)
Discussion started by: boaz733
5 Replies

7. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

8. Shell Programming and Scripting

add number in lines line by line in different files

I have a set of log files that are in the following format ======= set_1 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_2 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_3 ======== counter : 315 counter2: 204597 counter3: 290582 Is... (6 Replies)
Discussion started by: grandguest
6 Replies

9. Shell Programming and Scripting

To add a number at the end of the line

Hi Folks, Using the Vi, how can I add a numbers at the end of the line. For eg: I have the numbers in the file as: 58.125.33 22.58.68 25.144.225 114.25.38 I need to add .0/8 at the end of all the line. So, it should be like 58.125.33.0/8 22.58.68.0/8 25.144.225.0/8 114.25.38.0/8 (6 Replies)
Discussion started by: gsiva
6 Replies

10. Shell Programming and Scripting

how to add Loop number to variable name?

Hi there, I need to add a sequence of numbers to existing variables within the script to be able to call the variable automatically. Somehow it's just printing the loop number. Here's what I've got so far. :eek: #!/bin/sh FOLDER1=foo FOLDER2=bar FOLDER3=beer for ((a=1; a <= 3 ; a++))... (2 Replies)
Discussion started by: siul0_0
2 Replies
Login or Register to Ask a Question