Same sed code prints(p) correct, but writtes(w) wrong output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Same sed code prints(p) correct, but writtes(w) wrong output
# 1  
Old 07-11-2016
Same sed code prints(p) correct, but writtes(w) wrong output

Dear all,

I am using sed as an alternative to grep in order to get a specific line from each of multiple files located in the same directory. I am using sed because it prints the lines in the correct order (unlike grep).
When I write sed code that prints out the output I get it correct, but when I put it in a file, I get it only for one half of the files, and the other half is missing Smilie
Here is the code that 1 prints it out 2/3 puts the output into a file, but only one half of it:
Code:
for p in $(cat $HOME/file_with_numbers.txt); do cat $HOME/file_name${p}_* | sed -n 's/RUN_NAME_FOLDER.*/&/p'; done



for p in $(cat $HOME/  file_with_numbers.txt); do cat $HOME/file_name${p}_* | sed -n 's/RUN_NAME_FOLDER.*/&/w data.txt'; done



for p in $(cat $HOME/file_with_numbers.txt); do cat tsv/file_name${p}_* | sed -n 's/RUN_NAME_FOLDER.*/&/p' > data.txt; done

I don't understand why this happens and would really appreciate your help.

Last edited by Scrutinizer; 07-11-2016 at 12:28 PM.. Reason: icode tags + formatting => code tags
# 2  
Old 07-11-2016
The methods you are using are overwriting the output file several times.
Either append the file with >> or better yet, redirect the loop (after the done statement):

Code:
while read p
do
  sed -n 's/RUN_NAME_FOLDER.*/&/p' file_name${p}_*
done < file_with_numbers.txt > data.txt


--
By the way..
Code:
sed -n 's/RUN_NAME_FOLDER.*/&/p' file_name${p}_*

is
equivalent to
Code:
sed -n '/RUN_NAME_FOLDER/p' file_name${p}_*

I cannot understand how sed would produce a different order than grep would
Try replacing it with with
Code:
grep 'RUN_NAME_FOLDER' file_name${p}_*

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output is not the correct count

The awk below runs and produces the following output on the file2. This is just an example of the format as the file is ~14MB. file1.txt is attached. I am trying to count the ids that match between the two files and out the ids that are missing. Thank you :). file2 970 NM_213590 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Now showing the correct output

Hello I am working on one script where I am trying to display all the directories which is inside the workspace but somehow it is giving me weird output and this is occurring only with one directory other also having the result.html file inside the directory. for i in `ls -1 | egrep -iv... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

3. Shell Programming and Scripting

Html output in correct format

Hi, I am running two scripts as below. In Script 1 i am getting correct output in proper HTML format while in script 2 i am not getting output in mail and only html code is getting printed.I want to get the output of script 2. Please guide. 1.IFILE=/home/home01/Report.csv if #Checks... (7 Replies)
Discussion started by: Vivekit82
7 Replies

4. Shell Programming and Scripting

How to print the output in correct order?

Hi, while using following awk commend I’m getting confused, The output is not like as the row present in input files, can anyone explain and tell me how to print in the order like in input. value=$(awk 'FNR>1 && NR==FNR{a=$4;next} a{sum+=$4} END {for(i in sum){printf i"\t"sum/2"@@";}}'... (5 Replies)
Discussion started by: Shenbaga.d
5 Replies

5. Shell Programming and Scripting

Script which telnets to a device, runs commands and prints output to a file

I am connecting to a device using telnet, I want my script to perform certain commands : ie- show device , show inventory..etc and write the output it sees from the terminal to a file. this is what I have got : #!/usr/bin/expect -- set running 1 spawn telnet <ip address> expect ... (1 Reply)
Discussion started by: samantha123
1 Replies

6. Shell Programming and Scripting

Script to compare 2 files and prints difference as output sidebyside

Hi All, Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display. Basically line by line comparision and files may contain blank line as well I know we have compare/diff commands but i don't how to make... (10 Replies)
Discussion started by: Optimus81
10 Replies

7. Solaris

run a service via startup script (correct me if I am wrong)

Environment Solaris 9 I have configured the Solaris9 as NTP client in which Solaris9 is syncing the time with a windows2008 R2 Server which is runing fine. Now I want that the xntpd service should start at startup. I did this via a script. Kindly correct if I did any thing wrong: 1.)Made... (9 Replies)
Discussion started by: z_haseeb
9 Replies

8. Shell Programming and Scripting

Check my script and correct the find command if wrong

Hello All, Here I am trying to find all the directories whose name starts with EFS or HOTFIX like in below example and below is my code but I don’t know why this is not working correctly. drwxr-xr-x 3 qabuild denccefs 4096 Sep 23 21:36 EFS110929A_SOURCE/ -rwxrwxr-x 1 qabuild... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

9. Shell Programming and Scripting

diff output is it correct??

I'm asking for explanation about the output of the diff format when i compare the two files f1 and f2: root@host1 # cat f1 205226 205237 205250 205255 205262 205274 205307 205403 205464 205477 205500 205520 205626 205759 205766 205776 (2 Replies)
Discussion started by: ahmad.zuhd
2 Replies

10. Shell Programming and Scripting

Getting the correct identifier in the output file

Hi All I do have a file like this: 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 8.9e+016 GCGGTAACT 4 20 26 232 1.7e+013 TTTTTATTTTTTTTTTTTCC 5 ... (6 Replies)
Discussion started by: Lucky Ali
6 Replies
Login or Register to Ask a Question