Creating new file inside a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating new file inside a for loop
# 1  
Old 09-09-2015
Creating new file inside a for loop

Hi,

I have list of files present in a folder. I want to search for a particular keyword sequentially and create a file which will be later used by some other program.
Input files:
Code:
[mac]$ ls
a.dsx  b.dsx  c.dsx  Dataline_.txt  Dataline.txt  loop.sh
[ mac]$ cat *.dsx
help

tyiis
global

Code:
for i in `ls *.dsx`
do
grep 'help' $i > Dataline.txt;
mv Dataline.txt Dataline_$1.txt;
done

When i am executing this, i can see grep command is getting executed for all the files sequentially but the output is not getting captured in Dataline.txt

Code:
ls *.dsx
++ ls a.dsx b.dsx c.dsx
+ for i in '`ls *.dsx`'
+ grep help a.dsx
+ mv Dataline.txt Dataline_.txt
+ for i in '`ls *.dsx`'
+ grep help b.dsx
+ mv Dataline.txt Dataline_.txt
+ for i in '`ls *.dsx`'
+ grep help c.dsx
+ mv Dataline.txt Dataline_.txt

Can somebody point out as what i am doing wrong.

Cheers!!!

---------- Post updated at 11:05 AM ---------- Previous update was at 10:46 AM ----------

Sorry, I found the solution. The script is working as expected.

Code:
for i in `ls *.dsx`
do
grep 'help' $i > Dataline.txt;
cat Dataline.txt;
awk '{ print FNR "|" $0 }' Dataline.txt > DataLine1.txt;
cat DataLine1.txt;
done

The above is working code. Thanks
# 2  
Old 09-09-2015
Why not simply:
Code:
#(edit: for the first step)
grep 'help' *.dsx > Dataline.txt

Or even (untested, since you already solved it):
Code:
#(edit: as the 'all-in-one' solution')
awk '/help/ {print FNR,"|",$0}' *.dsx > Dataline1.txt

hth

Last edited by sea; 09-09-2015 at 03:04 AM.. Reason: changed code
# 3  
Old 09-09-2015
Try
Code:
grep 'help' *.dsx | nl -w1 -nln -s "|" > DataLine1.txt

---------- Post updated at 12:19 AM ---------- Previous update was at 12:13 AM ----------

Quote:
Originally Posted by sea
Code:
#(edit: as the 'all-in-one' solution')
awk '/help/ {print FNR,"|",$0}' *.dsx > Dataline1.txt

I believe this would be the equivalent output to the original line counter.
Code:
awk '/help/ {++c; print c "|" $0}' *.dsx > Dataline1.txt

# 4  
Old 09-10-2015
Try this ...

Code:
for i in *.dsx; do grep `help` "$i" ;  cp "$i" Dateline_$1.txt ; done

The idea is to put grep information into varible then copy the variable to the txt file
Moderator's Comments:
Mod Comment Please use CODE tags for sample input, sample output, and code segments.

Last edited by Don Cragun; 09-10-2015 at 01:58 AM.. Reason: Add CODE tags.
# 5  
Old 09-10-2015
Quote:
Originally Posted by jasahasch
Try this ...

Code:
for i in *.dsx; do grep `help` "$i" ;  cp "$i" Dateline_$1.txt ; done

The idea is to put grep information into varible then copy the variable to the txt file
Moderator's Comments:
Mod Comment Please use CODE tags for sample input, sample output, and code segments.
Note that you need single quotes around the help not backquotes. Putting help in backquotes executes the help utility and then invokes grep with the first "word" of the output from the help command as string to be searched and any remaining "words" (if there are any) as the names of files to be searched (along with the file named by the expansion of $i). I presume that what you meant was:
Code:
for i in *.dsx; do grep 'help' "$i" ;  cp "$i" Dateline_$1.txt ; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

2. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

3. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

4. Shell Programming and Scripting

Creating loops inside a file and extracting and loading data

Help needed (1 Reply)
Discussion started by: Chand Shrestha
1 Replies

5. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

file creation inside loop

i=1 while do touch TC$i.txt i=`expr $1 + 1` done echo "files created successfully" What is wrong with code...its running continuosly... (4 Replies)
Discussion started by: villain41
4 Replies

10. Shell Programming and Scripting

Creating a file inside a script

Hi, How can I create a file inside a script. The thing is that, I have to create the file inside the script and it will be acting as a log file the script.Whenever I execute the script the file has to be created and the output has to be directed into that file. Tahnks in advance (11 Replies)
Discussion started by: sendhilmani
11 Replies
Login or Register to Ask a Question