Using records iteratively with increament


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using records iteratively with increament
# 1  
Old 04-28-2012
Using records iteratively with increament

I am very new to unix. Trying to use a for loop for this task

I have a file like this

Code:
abc.uml
ccc.uml
ddd.uml
rrr.uml
kkk.uml
.
.
.
n.uml ( more than 10000)

I want to read this file but just 5 names at a time and then again it should read next 5 names and agin next 5. this should go on till the end of file. What ever it read it should be directed in the output file.

The code I am using

Code:
 for i in `cat input_file` ; doecho $i done



The output should be

Code:
hello there abc.uml
                 ccc.uml
                 ddd.uml
                 rrr.uml
                 kkk.uml
hello there hhh.uml
                 yyy.uml
                 eee.uml
                 qqq.uml
                 fff.uml
hello there iii.uml
                 ttt.uml
                 aaa.uml
                 sss.uml
                 zzz.iml
                 nnn.uml
hello there adn.uml
.
.
.
(till 10000 file names)

Please help me with this friends

Last edited by Scrutinizer; 04-28-2012 at 01:39 PM.. Reason: code tags or data files
# 2  
Old 04-28-2012
Assuming you're using Kshell or bash, this would be one way:

Code:
i=0
while read buffer
do
    (( i++ ))
    case $i in
        1) printf "hello there ";;  # print "leader text" on first of each group
        5) i=0;;             # last line of the group, reset counter
        *) ;;                # no special action needed; do nothing when i is 2,3,4
    esac

    printf "%s\n" "$buffer"    # always print the buffer
done <input-file

# 3  
Old 04-28-2012
Hello agama,

Sorry for the misunderstanding created by me
The code is working fine, just small modification.

The output has to be in this format

Code:
hello there abc.uml,
                 ccc.uml,
                 ddd.uml,
                  rrr.uml,
                kkk.uml with delimiter '|';
hello there hhh.uml,
                 yyy.uml,
                  eee.uml,
                  qqq.uml,
                   fff.uml with delimiter '|';
hello there iii.uml,
                ttt.uml,
              aaa.uml,
                sss.uml,
                zzz.iml with delimiter '|';
hello there adn.uml,
                 nnn.uml


Please help me with this format.

Moderator's Comments:
Mod Comment Please click this link: How to use [code][/code] tags

Last edited by Scrutinizer; 04-28-2012 at 02:42 PM..
# 4  
Old 04-28-2012
Small change to accomplish that:

Code:
i=0
while read buffer
do
    (( i++ ))
    case $i in
        1)  printf "hello there %s,\n" "$buffer";;  # print first
        5)  printf "%s |\n" "$buffer"               # print last and reset
            i=0
            ;;
        *) printf "%s,\n" "$buffer";;               # print middles with commas
    esac
done <input-file



Didn't know if you need/want a space before the pipe sym, you can remove it easly if it's not needed.
# 5  
Old 04-29-2012
Perfectly working now
Thanks for sharing your great knowledge.

---------- Post updated 04-29-12 at 12:13 PM ---------- Previous update was 04-28-12 at 11:28 PM ----------

I am trying to modify the given script to add some text in the output.

The output will be as follows

Code:
hello there /dev/pts/data abc.uml on node0,
                 /dev/pts/data ccc.uml on node0,
                 /dev/pts/data ddd.uml on node0,
                 /dev/pts/data rrr.umlon node0,
                /dev/pts/data kkk.uml on node0with delimiter '|';
hello there /dev/pts/data hhh.uml on node0,
                 /dev/pts/data yyy.uml on node0,
                  /dev/pts/data eee.uml on node0,
                  /dev/pts/data qqq.uml on node0,
                   /dev/pts/data fff.uml on node0 with delimiter '|';
hello there /dev/pts/data iii.uml on node0,
                /dev/pts/data ttt.uml on node0,
               /dev/pts/data aaa.uml on node0,
                /dev/pts/data sss.uml on node0,
                /dev/pts/data zzz.iml on node0 with delimiter '|';
hello there /dev/pts/data adn.uml on node0,
                 /dev/pts/data nnn.uml on node0,
.
.
.
.
.

Please help me with this modification

Last edited by Scrutinizer; 04-29-2012 at 03:55 AM.. Reason: code tags
# 6  
Old 04-29-2012
I think that if you experiment a little with the printf statements you should be able to do this yourself. What have you tried so far?
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-30-2012
I have done some modifications and I got the expected output. Thanks to all.
This User Gave Thanks to nnani For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace increament

Dears, Can you help me with the below concern, I need one liner for this. Input_file.txt dn: asdf=yyyyyyyyyyyyy426019001711473,ou=multiSCs,dc=btc asdf: yyyyyyyyyyyyy426019001711473 DSUnitGroup: 1 objectClass: CUDBMultiServiceConsumer dn:... (3 Replies)
Discussion started by: Kamesh G
3 Replies

2. Shell Programming and Scripting

Read vars iteratively

Hello, I have a tab delimited list of 311 server & account names which I want to read those 2 variables and then connect to each server and get info on that particular job account. I've tried the following: while read server acct; do printf "********$server\t $acct***********\n" ... (3 Replies)
Discussion started by: mcbobolink
3 Replies

3. Shell Programming and Scripting

Pairing the nth elements on multiple lines iteratively

Hello, I'm trying to create a word translation section of a book. Each entry in the word list will come from a set of linguistically analyzed texts. Each sentence in the text has the following format. The first element in each line is the "name" of the line (i.e. "A","B","C","D"). The... (12 Replies)
Discussion started by: John Lyon
12 Replies

4. Shell Programming and Scripting

Bin iteratively based on each row

Hi I have a file some thing like below. I want to bin the data. My Bin size is 100. items number HELIX1 75 HELIX6 160 HELIX2 88 HELIX19 114 HELIX5 61 HELIX4 167 it should consider each elemet under the number column and bin all the lines like below with 100... (7 Replies)
Discussion started by: and_y
7 Replies

5. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

6. UNIX for Dummies Questions & Answers

using awk iteratively in a script to assign variable values

I have a log file that has certain fields that I want to evaluate, and depending on the value in those fields, I want to put the value of a different field in that line in a particular variable that I'll use later on down the log file. Sort of like setting a switch to change what I do with a bunch... (5 Replies)
Discussion started by: pts2
5 Replies

7. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

8. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

9. UNIX for Dummies Questions & Answers

How to iteratively copy *.jar files

Hello, I want to copy all *.jar files from a parent tree structure, traversing all sub directories, into the traget dir. What shud be the command for that. I tried below command from the source parent dir cp -r *.jar pathtotargetdir but get the error '*.jar : No such file or... (1 Reply)
Discussion started by: New2
1 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question