2 Loops gathering input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2 Loops gathering input
# 8  
Old 04-05-2017
Implied double loop - bash 4 needed:
Code:
eval printf "%s\\\n" {$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file1)}\" \"{$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file2)}
folder1 Tom
folder1 Harry
folder1 Sally
folder1 Joe
folder2 Tom
folder2 Harry
folder2 Sally
folder2 Joe
folder3 Tom
folder3 Harry
folder3 Sally
folder3 Joe

Beware of the evil eval for the usual, well known reasons...
# 9  
Old 04-05-2017
Quote:
Originally Posted by RudiC
Implied double loop - bash 4 needed:
Code:
eval printf "%s\\\n" {$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file1)}\" \"{$(sed -n '1h; 1!H; ${g;s/\n/,/g; p; }' file2)}
folder1 Tom
folder1 Harry
folder1 Sally
folder1 Joe
folder2 Tom
folder2 Harry
folder2 Sally
folder2 Joe
folder3 Tom
folder3 Harry
folder3 Sally
folder3 Joe

Beware of the evil eval for the usual, well known reasons...
Still getting the same error. Here is the output I am getting with that:

Code:
{P.P..Pfolders.txtProups.txt} {P.P..Pfolders.txtProups.txt}

Here is my script currently, the list of folders is being added to an XML file, and i need to add groups too. take a look (Bold is what I need to add):

Code:
		   while read line
			   do
			   folder=${line%}
		       echo "<folder name = \"$folder\" group name = \"$group\">" >> output.xml
			   echo "</folder>" >> output.xml
		    done < folders.txt

This is AIX by the way

---------- Post updated at 02:43 PM ---------- Previous update was at 01:54 PM ----------

I think I may be overthinking this. I divided this into two seperate loops:

Code:
while read line
do
group=${line%}
echo "<group=\"$group\">" >> Output.xml
done < groups.txt


while read line
do
group=${line%}
echo "<group=\"$group\">" >> Output.xml
done < groups.txt

My output looks as expected:

Code:
<folder name = "Folder3">
</folder>
<folder name = "Folder2">
</folder>
<folder name = "Folder1">
</folder>
<folder name = "Folder0">
</folder>
<file group="TestGroup3">
</file>
<file group="TestGroup2">
</file>
<file group="TestGroup1">
</file>

Now, I need to get each of those 3 groups after the start and end tag of folder..
# 10  
Old 04-05-2017
wouldn't it be easier in awk:
Code:
awk 'FNR==NR {f[$0];next} {for (i in f) print i,$0}' folderFile userFile


Last edited by vgersh99; 04-05-2017 at 09:46 PM..
# 11  
Old 04-05-2017
Quote:
Originally Posted by vgersh99
wouldn't it be eassier in awk:
Code:
awk 'FNR==NR {f[$0];next} {for (i in f) print i,$0}' folderFile userFile

Yeah, I have played with a few things in awk and sed but I cannot get a viable solution.

where in that snippet that you provided would I place where I want to place the inserted line?

Currently have:

Code:
<folder name = "Folder3">
</folder>



Need:

Code:
<folder name = "Folder3">
    <file group="TestGroup1">
    </file>
</folder>



Thanks!
# 12  
Old 04-05-2017
how about:
Code:
awk -v qq='"' '
   FNR==NR {u[$0];next}
  {
      printf("<folder name = %s%s%s>\n",qq,$0,qq)
      for (i in u) 
         printf("\t<file group =%s%s%s></file>\n",qq,i,qq)
      print "</folder>"
  }' userFile.txt folderFile.txt


Last edited by vgersh99; 04-05-2017 at 05:20 PM.. Reason: edited for readibility
This User Gave Thanks to vgersh99 For This Post:
# 13  
Old 04-05-2017
Quote:
Originally Posted by vgersh99
how about:
Code:
awk -v qq='"' '
   FNR==NR {u[$0];next}
  {
      printf("<folder name = %s%s%s>\n",qq,$0,qq); for (i in u) printf("\t<file group =%s%s%s></file>\n",qq,i,qq); print "</folder>"
   }' userFile.txt folderFile.txt

Looks like that works, help is much appreciated to everyone in this post that helped me!! Smilie
# 14  
Old 04-05-2017
Code:
$
$ cat folders.txt
Folder3
Folder2
Folder1
$
$ cat groups.txt
TestGroup3
TestGroup2
TestGroup1
$
$ perl -lne '$s ? do{push @g,$_} : do{push @f,$_; $s=1 if eof} }{
             for $i (@f){
                 print "<folder name = \"$i\">";
                 for $j (@g){ print "    <file group = \"$j\"></file>"}
                 print "</folder>"
             }' folders.txt groups.txt
<folder name = "Folder3">
    <file group = "TestGroup3"></file>
    <file group = "TestGroup2"></file>
    <file group = "TestGroup1"></file>
</folder>
<folder name = "Folder2">
    <file group = "TestGroup3"></file>
    <file group = "TestGroup2"></file>
    <file group = "TestGroup1"></file>
</folder>
<folder name = "Folder1">
    <file group = "TestGroup3"></file>
    <file group = "TestGroup2"></file>
    <file group = "TestGroup1"></file>
</folder>
$
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gathering usage statistics

so i have a script that runs across many servers. i'd like to know how many times this script is being used on each server. the only straight forward, non-intrusive way i can think of doing this is to include a line in the script to make a webcall to a central server. and from that central... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Gathering data in columns from multiple files

Hello world! I need to gather all the data from different folders and copy it in an one unique text file in columns format. Let me explain, letīs say "a, b, c" are 3 data files of thousands and thousands lines (in the reality, I have nearly one hundred). "a, b, c" are located in the folders... (5 Replies)
Discussion started by: user10600
5 Replies

3. UNIX Desktop Questions & Answers

awk using 2 input files instead of while loops

Hi Friends, I have two files as input with data that looks like this: file1.txt 1 2 3 4 file2.txt a,aa b,bb c,cc d,dd e,ee f,ff instead of me doing 2 while loops to get the combinations while read line_file1 (2 Replies)
Discussion started by: kokoro
2 Replies

4. Red Hat

What is the best tools for performance data gathering and analysis?

Dear Guru, IHAC who complaint that his CentOS is getting performance issue. I have to help him out of there. Could you please tell me which tools is better to gathering the whole system performance data? -- CPU/Memory/IO(disk & Network)/swap I would like the tools could be... (6 Replies)
Discussion started by: devyfong
6 Replies

5. Shell Programming and Scripting

Kornshell gathering user input question

Alright I have a function that does a bunch of commands and then I ask the user for what type of node they are on. So determining which node they are on means they will run a different function. Whats the correct syntax for that? function everything { echo "do stuff" }... (10 Replies)
Discussion started by: Blogger11
10 Replies

6. UNIX for Advanced & Expert Users

Gathering data using SAR

Hi everyone, I would like to ask if it is possible to gather SAR data on a specified time. let say yesterdays report, i want to get data at around 12PM and 5PM. thank you. (2 Replies)
Discussion started by: cwiggler
2 Replies

7. HP-UX

HP-UX configuration gathering tool

Hello! I would like to introduce a tool for gathering information on the HP- UX operating system. I would like to hear experts opinions about this utility, its prospects and usefulness. Any feedbacks, suggestions, bug reports, feature requests etc are welcome. The tool project web page:... (0 Replies)
Discussion started by: ahaidukov
0 Replies

8. Shell Programming and Scripting

Data gathering script

I am pretty new at shell scripting, but I managed to make a primative shell script to connect from my webserver (bluehost) to an external data server via ftp or sftp or whatever I need. This script is : #!/bin/sh HOST='ftp.host.com' USER='username' PASSWD='password' FILE='file' ftp -n... (7 Replies)
Discussion started by: mesoeast
7 Replies

9. UNIX for Dummies Questions & Answers

Gathering system configuration

Hi there, I have been asked to write a script that gathers enough information on our Sun Solaris machines to be able to rebuild and configure them if they should go pop. My question is does anybody have any suggestions on the files that I need to take a copy of, to ensure that everything is... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

10. Shell Programming and Scripting

Gathering info on one line

Hi all again, here is the file i am working on : GREIMBAJ00;BAN_CAV;Loader.sh;2003/06/13;17:04:04 GREIMBAJ00;BAN_CAV;Loader.sh;2003/06/13;17:04:06 GREIMBAJ00;BAN_PAK;Loader.sh;2003/06/13;17:04:11 GREIMBAJ00;BAN_PAK;Loader.sh;2003/06/13;17:04:18... (5 Replies)
Discussion started by: HowardIsHigh
5 Replies
Login or Register to Ask a Question