Need help with looping through file contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with looping through file contents
# 1  
Old 06-17-2015
Error Need help with looping through file contents

Hi,

I have a several files with ".out" extension in a folder and each file has the below like contents
Code:
....
appname: wonder1
.....
logname78.log
logname88.log
.....
CAP: 2
.....
appname: wonder54
......
logname28.log
logname58.log
logname54.log
.....
CAP: 3
.....
appname: wonder11
.....
logname92.log
.....
CAP: 1
.....

Where "...." can be bla bla bla any string.
Below is the desired output
Code:
wonder1


wonder54



wonder11


logname78.log
logname88.log

logname28.log
logname58.log
logname54.log

logname92.log

I run a for loop to traverse through the folder picking one file at a time.

Code:
for file in *.out
do
        for csfile in $(grep -F 'appname' $file | awk '{print $2}')
        do
                echo $csfile
                for counter in $(grep -F 'CAP:' $file | awk '{print $2}')
                do
                        let start=1
                        for i in {$start..$counter}
                        do
                                echo " "
                        done
                done
        done
        for logg in $(grep -F '.log' $file)
        do
             echo $logg
        done
     
echo " "
done

The above effort does not yield what i desire ...

Problem 1: The value of CAP remains always the same i.e the first vaue in the file.

Problem 2: The .log files are not seperated by blank lines as seen in the desired output.

Current output looks something like this

Code:
wonder1


wonder54


wonder11


logname78.log
logname88.log
logname28.log
logname58.log
logname54.log
logname92.log

I know the basics of loops but can u help me structure my code get the correct output.

Last edited by mohtashims; 06-17-2015 at 07:38 PM..
# 2  
Old 06-17-2015
I don't think that your output sample fits what your code snippet will print.
You might want to look into this and check if you could adopt and adapt:
Code:
awk '
/CAP/           {L=L ORS
                 for (i=1; i<=$2; i++) print ""
                }
/appname/       {print $2}
/^log/          {L=L $0 ORS}
END             {print L}
' $file
wonder1


wonder54



wonder11

logname78.log
logname88.log

logname28.log
logname58.log
logname54.log

logname92.log

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-17-2015
Quote:
Originally Posted by RudiC
I don't think that your output sample fits what your code snippet will print.
You might want to look into this and check if you could adopt and adapt:
Code:
awk '
/CAP/           {L=L ORS
                 for (i=1; i<=$2; i++) print ""
                }
/appname/       {print $2}
/^log/          {L=L $0 ORS}
END             {print L}
' $file
wonder1


wonder54



wonder11

logname78.log
logname88.log

logname28.log
logname58.log
logname54.log

logname92.log

I will test it tmr, however is that another way to get the desired output.
# 4  
Old 06-17-2015
From bash you could do (Based on RudiC's awk script):

Code:
for file in *.out
do
   log=""
   while read line
   do
      case "$line" in
          appname:\ *) echo "${line#* }"  ;;
          logname*)    printf -v log "%s%s\n" "$log" "$line" ;;
          CAP:\ *)     printf -v log "%s\n" "$log"
                       eval printf '%.0s\\n' {1..${line#* }} ;;
      esac
   done < $file
   printf "%s" "$log"
done

Edit: And if you don't like using eval (which can be wise) replace the eval line with:

Code:
declare -a "vals=({1..${line#* }})"
printf "%.0s\n" ${vals[@]} ;;


Last edited by Chubler_XL; 06-17-2015 at 09:40 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 06-17-2015
A variation from Chubler_XL's post

Code:
br() {
    for (( c=1; c<=${1}; c++ )); do
        echo 
    done
}

for file in *.out; do
   log=""
   while read field1 field2; do
       case "$field1" in
           appname:)   echo "${field2}"  ;;
           logname*)   printf -v log "%s%s\n" "$log" "$field1" ;;
               CAP:)   printf -v log "%s\n" "$log"
                       br "${field2}" ;;
      esac
   done < "$file"
   printf "%s" "$log"
done


Last edited by Aia; 06-18-2015 at 12:06 AM.. Reason: Added quotes
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Looping through the contents of array for output name

Hi all, I am trying to loop through the string contents of an array, to add it during the saving of the output files. I am trying this code to print each column and save it to unique file name, but it doesn't work. Thanks for any help. fnam=(japan usa uk) alldata.dat contained sample data... (1 Reply)
Discussion started by: ida1215
1 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. UNIX for Dummies Questions & Answers

Looping/Reading file contents not working

Hi, I am doing something basic, but I am missing something. Im trying to read the contents of a file and taking those values and connecting to a database. However, it only connect to one (or reads in) value and then exists. Here is what it looks like: listname.txt db1 db2 db3 Script:... (15 Replies)
Discussion started by: DBnixUser
15 Replies

4. Shell Programming and Scripting

Looping inside directories based on a file which contains file directory list

Hi All, Please help. I have got a file which contains a listing of a file and some directories after it, one by one. I am calling this file xyz.txt here file1 dir1 dir2 dir3 dir4 file2 dir5 dir6 dir7 dir8 file3 dir9 dir10 dir11 dir12 (6 Replies)
Discussion started by: Piyush Jakra
6 Replies

5. Shell Programming and Scripting

FILE_ID extraction from file name and save it in CSV file after looping through each folders

FILE_ID extraction from file name and save it in CSV file after looping through each folders My files are located in UNIX Server, i want to extract file_id and file_name from each file .and save it in a CSV file. How do I do that? I have folders in unix environment, directory structure is... (15 Replies)
Discussion started by: princetd001
15 Replies

6. Shell Programming and Scripting

While or For with looping contructs? to create files from contents of a text file

Hi so far I created this script: vi loop.beta.sh for i in `cat extract.filenames.tabc` do echo $i done>$i === This is the original text file. $ more tabc.txt -rwx------- 1 alice staff 1586 2010-11-05 02:27 request-key .conf -rwx------- 1 ted staff 126 ... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

7. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

8. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

9. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

10. Shell Programming and Scripting

Creating file contents using contents of another file

Hi, I am not sure how to start doing this so I hope to get some advice as to how to start. I have 2 files. The source file contains data that I needed is in columns delimited by ";". For example, in this format: "CONTINENT","COUNTRY","CITY","ID" "asia","japan","tokyo","123"... (21 Replies)
Discussion started by: ReV
21 Replies
Login or Register to Ask a Question