printing strings in one X number of times from another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing strings in one X number of times from another
# 1  
Old 06-16-2008
printing strings in one X number of times from another

I have one file of numbers

4
5
2
...

And another file of strings

aaaaa bbbbb
ccccc ddddd
eeeee ffffff
...

I'd like to print the stings from each line in reverse order with some decoration the number of times listed in the first file

such as:

Yeah bbbbb aaaaa
Yeah bbbbb aaaaa
Yeah bbbbb aaaaa
Yeah bbbbb aaaaa
Yeah ddddd ccccc
Yeah ddddd ccccc
Yeah ddddd ccccc
Yeah ddddd ccccc
Yeah ddddd ccccc
Yeah fffff eeeee
Yeah fffff eeeee


I'm trying with:

awk '
NR==FNR{
a[NR]=$0
next
}
{
for(i=1;i<NR;i++)
print "Yeah", $2,$1
}
' filenumbers filestrings > outfile

This isn't working.

Thanks for your help
# 2  
Old 06-16-2008
It is unclear what the file with numbers is for. How does "Yeah" derive from the number "5" or the number "4"?
# 3  
Old 06-16-2008
followup

Sorry. The number in line one of the file with numbers is the number of times I'd like to print out the manipulation of the strings in line one from the file with the strings. The 'Yeah' is an example of appended text that doesn't appear in the file with strings that I'd like to add to each line of the outfile.

For example if '5' appears in line one of the file with numbers and '2' appears in line 2 of the file with numbers I'd like to print out the modified stings in line one of the file with strings 5 times followed by the modified strings from line 2 two times

File with numbers:
5
2

_____

File with strings:
aa bb
cc dd
_____

Example outfile:

1) bb aa
2) bb aa
3) bb aa
4) bb aa
5) bb aa
1) dd cc
2) dd cc

Thank you.
# 4  
Old 06-16-2008
Your awk script never uses a[index] for anything after declaring it. You probably want the loop condition to read i <= a[NR]

For what it's worth, I don't think the original problem description was unclear at all.
# 5  
Old 06-16-2008
Code:
awk '{
   if (NR != FNR) {
      for (i = 1; i <= f[FNR]; ++i) {
         printf("Yeah")
         for (j = NF; j >= 1; --j)
            printf(" %s", $j)
         print ""
      }
   } else
      f[NR] = $1
}' filenumbers filestrings

# 6  
Old 06-16-2008
Thank you

Thank you so much that worked great. As you can tell I'm very new to this. SmilieSmilieSmilieSmilieSmilieSmilie

One thing to avoid is switching the files LOL

awk '{
if (NR != FNR) {
for (i = 1; i <= f[FNR]; ++i) {
printf("Yeah")
for (j = NF; j >= 1; --j)
printf(" %s", $j)
print ""
}
} else
f[NR] = $1
}' filestrings filenumbers >out

and walk away...

Good way to fill up your drive
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print String N times the number before it

Hey All, I want want to print a string N times the number N before it. Like i have "20 hello". so i want to print hello hello hello . . . . . 20 times.. Please help me.. I am not able o figure out.. how to do the same? (8 Replies)
Discussion started by: jaituteja
8 Replies

2. Shell Programming and Scripting

Count how many times in every file, strings appeared in a directory.

Hello, I have some files and i want to count how many times a string is appeared in each file. Lets say : #cat fileA stringA sdh stringB stringA #cat fileB stringB stringA sdb stringB stringB I need the output to be something like: (2 Replies)
Discussion started by: @dagio
2 Replies

3. Shell Programming and Scripting

Printing Number of Fields with the line number

Hi, How to print the number of fields in each record with the line number? Lets saw I have 3212|shipped|received| 3213|shipped|undelivered| 3214|shipped|received|delivered I tried the code awk -F '|' '{print NF}' This gives me ouput as 3 3 4 (5 Replies)
Discussion started by: machomaddy
5 Replies

4. Programming

Printing same strIng many times

In python how we need to print a same string many times without using loop. I cane across something like * operator for this . How we Can use this in a print statement ? I am using python 3.x Please help me (7 Replies)
Discussion started by: pandeesh
7 Replies

5. Shell Programming and Scripting

Problem with printing the strings

hello all, I am novice to shell scripting and need your help guys.I have a file called myfile.txt and its contents are shown below : <project name="device/samsung/crespo" revision="c932ac3b9f8a53a5c433dc7d3cfa2fb5d45a7eb9"/> <project name="device/samsung/crespo4g"... (7 Replies)
Discussion started by: acdc
7 Replies

6. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

7. Shell Programming and Scripting

Match and count the number of times

ile1 Beckham Ronaldo file2 Beckham Beckham_human Ronaldo Ronaldo_spain Ronaldo Ronaldo_brazil Beckham Beckham_manch Zidane Zidane_Fran Rooney Rooney_Eng Output shud be (1 Reply)
Discussion started by: cdfd123
1 Replies

8. Shell Programming and Scripting

printing each line in a file X times

This is probably simple but I would like to print every line in a file 5 times like this: awk 'NR' mens_csv.tab Dub Tank 53%Hemp/42%Cotton/5%Lycra Jersey Dark Green 0 $22.50 Dub Tank 53%Hemp/42%Cotton/5%Lycra Jersey Indigo Blue 0 $22.50 --------------- Dub... (4 Replies)
Discussion started by: Autumn Tree
4 Replies

9. UNIX for Dummies Questions & Answers

Counting the number of times a character appears

I am looking for a bash command that counts the number of times a character appears in a file. For example "I am a newbie, trying to learn shell script". Then the command counts the number of e and gives them as 4. Also I want one that counts the number of times a character in a string is replaced.... (2 Replies)
Discussion started by: #moveon
2 Replies

10. AIX

Number of login times

Hi! I'm currently using AIX 4.3 and would like to know where can i find to see that there's a restriction on the number of login times a user can have. Example, I want to see whether user A has only 1 login while user B can have 2 logins (without logging off the first one). Would I be able to... (7 Replies)
Discussion started by: ftengcheng
7 Replies
Login or Register to Ask a Question