Doubt in For loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Doubt in For loop
# 1  
Old 01-28-2014
Blade Doubt in For loop

Hi All,

I wrote a for loop where i am trying to delete the 1st line of each file and append then into a single file, my code looks like this but i am not sure if its right or worng . Can any one correct me if i am worng.

Code:
FILES=path/to/files/date_*.csv
for f in $FILES
do
   sed '1d' >> merge.csv
   done

# 2  
Old 01-28-2014
Pass filename as input to sed:
Code:
for f in path/to/files/date_*.csv
do
        sed '1d' "$f"
done > merge.csv

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-28-2014
Hi Yoda,

Thanks for the quick reply ,in the last step after done do This > append or overwrite the file (as far as my little knowledge in unix to append >> is used right) correct me if i am wrong.
# 4  
Old 01-28-2014
It overwrites the file. But you get the output of the entire loop in the file -- it gets opened once and written to lots of times.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-28-2014
Hello,

Here is an example for same.

Code:
for i in /home/Singhs1/singh_testing1211/reset_data_check1211
do
sed '1d' "$i" > OUTPUT
done


output will be in OUTPUT file.


Thanks,
R. Singh
# 6  
Old 01-28-2014
Thank you yoda and corona688
# 7  
Old 01-28-2014
Quote:
Originally Posted by RavinderSingh13
Hello,

...
Code:
for i in /home/Singhs1/singh_testing1211/reset_data_check1211
do
sed '1d' "$i" > OUTPUT
done

output will be in OUTPUT file.
...
I am afraid in this variation you have to use
Code:
>>

, as each invocation of sed process will overwrite the OUTPUT file and you will have results from the last file.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop/while condition - doubt

hi., As we know that using for-loop or while condition, we can only process one by one sequentially, but , lets say this example : 1. under the folder "logs" there are 1000 files 2. each file has one record or line 3. have to perform atleast 7 queries per 3 seconds ,for instance ... (3 Replies)
Discussion started by: alnhk
3 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. UNIX for Dummies Questions & Answers

Doubt

Hi , Struck with one basic question. Iam expecting word count of 4 where "wc" is showing as 5 . # echo "abcd" | wc 1 1 5 # echo abcd | wc 1 1 5 (5 Replies)
Discussion started by: penchal_boddu
5 Replies

5. UNIX for Advanced & Expert Users

doubt in df -h

in my parition i hav parition like this Filesystem Size Used Avail Use% Mounted on /dev/sda2 24G 22G 756M 97% / /dev/sda5 248G 1.2G 234G 1% /else /dev/sda1 965M 24M 892M 3% /boot tmpfs 7.0G 0 7.0G 0%... (1 Reply)
Discussion started by: ponmuthu
1 Replies

6. 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

7. Shell Programming and Scripting

Doubt??

I jus want to know the meaning of the below command line(exclamation following that re-direction) sqlplus -s `cat /home/sample.txt` <<! Thanks!! (1 Reply)
Discussion started by: nohup
1 Replies

8. Shell Programming and Scripting

One doubt

Hi, Can i use the shell script like this? When i am running the script it is hanging not giving me any output. I can redirect the output and then i can do the manipulations also but why this one is wrong. I am confused we can do like this or not.. #!/usr/bin/ksh for line in `top` do... (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. UNIX for Dummies Questions & Answers

doubt on name

HI, Iam jus tin a confusion that solaris and unix are the same.if they are diffrent,how they are?pls give me a brief idea abt these two. I will be very grateful to you thanks (1 Reply)
Discussion started by: shruti_mgp
1 Replies
Login or Register to Ask a Question