Sponsored Content
Full Discussion: Loop in shell script
Top Forums Shell Programming and Scripting Loop in shell script Post 302839401 by blackrageous on Thursday 1st of August 2013 11:15:20 AM
Old 08-01-2013
Your output doesn't seem consistent. I think you are probably intending to convert to what is called record format, where each line conveys it's "own context". I suggest awk not grep. Here is a starting point:
Code:
$ cat a.a
/A/B/C/abc.txt 
2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 
/A/B/C/xyz.txt 
2011-09-21 18:30:00,2013-07-30 06:15:00,15,65135 
2009-11-09 18:00:00,2011-09-02 09:00:00,5,12345 
2013-07-28 13:50:00,2013-07-31 01:00:00,5,710


$ awk '/^\//{file=$0}/^[0-9]/ {print file,$0}' a.a
/A/B/C/abc.txt  2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 
/A/B/C/xyz.txt  2011-09-21 18:30:00,2013-07-30 06:15:00,15,65135 
/A/B/C/xyz.txt  2009-11-09 18:00:00,2011-09-02 09:00:00,5,12345 
/A/B/C/xyz.txt  2013-07-28 13:50:00,2013-07-31 01:00:00,5,710

Here is the same example, but this time using an awk file, because placing awk script on the command line can sometimes be confusing
Code:
$ cat x.awk
/^\//    {file=$0}
/^[0-9]/ {print file,$0}


$ cat a.a
/A/B/C/abc.txt 
2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 
/A/B/C/xyz.txt 
2011-09-21 18:30:00,2013-07-30 06:15:00,15,65135 
2009-11-09 18:00:00,2011-09-02 09:00:00,5,12345 
2013-07-28 13:50:00,2013-07-31 01:00:00,5,710


$ awk -f x.awk a.a
/A/B/C/abc.txt  2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 
/A/B/C/xyz.txt  2011-09-21 18:30:00,2013-07-30 06:15:00,15,65135 
/A/B/C/xyz.txt  2009-11-09 18:00:00,2011-09-02 09:00:00,5,12345 
/A/B/C/xyz.txt  2013-07-28 13:50:00,2013-07-31 01:00:00,5,710

I did not account for the "searched pattern" part of your records, but this is a starting point only and hope the use of awk in this fashion can help. Look at BEGIN and END statements in awk as well.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If then else loop in Shell script

Hi Following is the code . When I give input as Bangalore,its dospalying Welcome to Hitech City. But say , if I select Delhi or US, its not displaying the corresponding message. Its still says Welcome to Hitech City. Seems that it not entering in the elif part. Please suggest. #!... (4 Replies)
Discussion started by: pankajkrmishra
4 Replies

2. Shell Programming and Scripting

Help with loop in a shell script

I just want to write a little script, that reads the lines from a file, echos somthing in a new tmp.file and then do some commands whith the tmp.files. while read -r line do echo "TEST=" > tmp.$$ echo "$line" >> tmp.$$ any_command < tmp.$$ done < $INPUTFILE But I think I have to... (2 Replies)
Discussion started by: elifchen
2 Replies

3. Shell Programming and Scripting

Loop in shell script

Dear experts, i am quite new to shell script please any one can help me in this regard i would like write a script which takes input in the form >./Test.sh a,10,b,20,c,30... in this way i can give input in any number which is not constant in the end through loop i want to... (3 Replies)
Discussion started by: vin_pll
3 Replies

4. Shell Programming and Scripting

Shell script using loop

Hi everyone, I have n number of data in my file "temp" in following order.In each line table_name and column_name are different.input data is in same format each query in three lines. ALTER TABLE table_name ADD ( column_name1 VARCHAR2(10), column_name2 VARCHAR2(70) ); ... (23 Replies)
Discussion started by: alisha
23 Replies

5. Shell Programming and Scripting

while loop problem in c shell script

Hi all, i write a script c shell set i = 1 while ( $i <= $#array ) echo "$array" @ i++ end i want to set it to i = i +2 in that statement . Can anybody help me? ---------- Post updated at 02:46 PM ---------- Previous update was at 02:35 PM ---------- anybody not how to solve it??? (2 Replies)
Discussion started by: proghack
2 Replies

6. Shell Programming and Scripting

Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message. ... (4 Replies)
Discussion started by: zero3ree
4 Replies

7. Shell Programming and Scripting

Avoiding For Loop in Shell Script

I am looking to a solution to the following problem. I have a very large file that looks something like this: Each group of three numbers on each line are three probabilities that sum to one. I want to output the maximum for each group of three. So desired output would be: or... (6 Replies)
Discussion started by: hydrabane
6 Replies

8. Shell Programming and Scripting

Help with the For loop shell script

Hi, I have multiple files in a directory. Each file will have a header.I have to check if any of the files has 0 rows other than the header then I have to delete the files. Here “ Empty file” in my case means a file has header information but no data. I have to delete such files. If the file... (2 Replies)
Discussion started by: ganesnar
2 Replies

9. Shell Programming and Scripting

Speed up the loop in shell script

Hi I have written a shell script which will test 300 to 500 IPs to find which are pinging and which are not pinging. the script which give output as 10.x.x.x is pining 10.x.x.x. is not pining - - - 10.x.x.x is pining like above. But, this script is taking... (6 Replies)
Discussion started by: kumar85shiv
6 Replies

10. Shell Programming and Scripting

Shell script use two variable in for loop

I want to create a shell script to add a user and modify its comment field mentioned in a file. 1. File value:- username comment field value xyz123 xyztesting abc123 abctesting def123 deftesting 2. i am using below loop to create user... (2 Replies)
Discussion started by: Anil
2 Replies
DIFF(1) 						      General Commands Manual							   DIFF(1)

NAME
diff - differential file comparator SYNOPSIS
diff [ -efbh ] file1 file2 DESCRIPTION
Diff tells what lines must be changed in two files to bring them into agreement. If file1 (file2) is `-', the standard input is used. If file1 (file2) is a directory, then a file in that directory whose file-name is the same as the file-name of file2 (file1) is used. The normal output contains lines of these forms: n1 a n3,n4 n1,n2 d n3 n1,n2 c n3,n4 These lines resemble ed commands to convert file1 into file2. The numbers after the letters pertain to file2. In fact, by exchanging `a' for `d' and reading backward one may ascertain equally how to convert file2 into file1. As in ed, identical pairs where n1 = n2 or n3 = n4 are abbreviated as a single number. Following each of these lines come all the lines that are affected in the first file flagged by `<', then all the lines that are affected in the second file flagged by `>'. The -b option causes trailing blanks (spaces and tabs) to be ignored and other strings of blanks to compare equal. The -e option produces a script of a, c and d commands for the editor ed, which will recreate file2 from file1. The -f option produces a similar script, not useful with ed, in the opposite order. In connection with -e, the following shell program may help maintain multiple versions of a file. Only an ancestral file ($1) and a chain of version-to-version ed scripts ($2,$3,...) made by diff need be on hand. A `latest version' appears on the standard output. (shift; cat $*; echo '1,$p') | ed - $1 Except in rare circumstances, diff finds a smallest sufficient set of file differences. Option -h does a fast, half-hearted job. It works only when changed stretches are short and well separated, but does work on files of unlimited length. Options -e and -f are unavailable with -h. FILES
/tmp/d????? /usr/lib/diffh for -h SEE ALSO
cmp(1), comm(1), ed(1) DIAGNOSTICS
Exit status is 0 for no differences, 1 for some, 2 for trouble. BUGS
Editing scripts produced under the -e or -f option are naive about creating lines consisting of a single `.'. DIFF(1)
All times are GMT -4. The time now is 10:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy