Treat Command Output as a File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Treat Command Output as a File
# 15  
Old 10-02-2014
Quote:
Originally Posted by aimy
[...]
At the moment, I use this command to avoid multiple lines command:
Code:
cut -c 1-25 source.list > source.temp && grep -vxFf source.temp complete.list > missing.list && rm -f source.temp

But the issue here is, the rm -f source.temp is only executed whenever the missing.list is not 0 in size.

Please help.

Thank you.
If that is an issue then try:

Code:
cut -c 1-25 source.list > source.temp && grep -vxFf source.temp complete.list > missing.list; [ -e source.temp ] && rm -f source.temp

---------- Post updated at 09:30 PM ---------- Previous update was at 08:49 PM ----------

I wonder if this will produce the same result you are looking for:
Code:
awk -F"@" 'NR==FNR {a[$1];next} {if($1 in a){next;}print $1}' incomplete.list source.list

This User Gave Thanks to Aia For This Post:
# 16  
Old 10-02-2014
Quote:
Originally Posted by Aia
If that is an issue then try:

Code:
cut -c 1-25 source.list > source.temp && grep -vxFf source.temp complete.list > missing.list; [ -e source.temp ] && rm -f source.temp

---------- Post updated at 09:30 PM ---------- Previous update was at 08:49 PM ----------

I wonder if this will produce the same result you are looking for:
Code:
awk -F"@" 'NR==FNR {a[$1];next} {if($1 in a){next;}print $1}' incomplete.list source.list

Thanks a lot!!

Yep this awk -F"@" 'NR==FNR {a[$1];next} {if($1 in a){next;}print $1}' source.list complete.list works! And it is so flexible and thus the difference could be done both ways. Thank you so much.

The temp file removal also works. Thanks.

So, could you explain both of the codes? What does the [ -e source.temp ] acts for?

Thanks again.
# 17  
Old 10-02-2014
If was possible to just do:

Code:
cut -c 1-25 source.list > source.temp && grep -vxFf source.temp complete.list > missing.list; rm -f source.temp

which will try to delete source.temp regardless of any other previous command, however, that would display an error if it was not found.

[ -e source.temp ] (notice the spaces after [ and before ], very important) is test. (take a look at man test), -e evaluates to true if exists, so if that is true then continue to rm -f, avoiding the error message.

Awk


-F"@" make the field separator an `@', that takes care of the format of source.list, dividing each line into two fields, before the `@' and after the `@'

NR==FNR {a[$1];next} this part is only for the first file, in this case source.list. The build-in variable NR is set to the total number of records found from all files, the build-in variable FNR is only the number of records in the current reading file, so as long as both are the same, we are reading only from the first file, if so perform the instructions on the first set of {a[$1];next} which only keeps an array named `a[name of the field]' and skip the rest of the instructions.

Now, we have the whole first file indexed in memory.

{if($1 in a){next;}print $1}'The rest is just an evaluation for the second file, if any of the records are seen already, do nothing (skip to next), otherwise, display it because is missing.

Last edited by Aia; 10-02-2014 at 02:39 AM.. Reason: more explicit
This User Gave Thanks to Aia For This Post:
# 18  
Old 10-02-2014
Thanks a lot @Aia

So in conclusion, there is no way for me to mimic the process substitution by any other means right?

Because...
Code:
$ ls /dev/fd
/dev/fd not found

Thanks a lot to all.
# 19  
Old 10-02-2014
Well, there might be. Try
Code:
cut -c 1-25 source.list | grep -vxFf - complete.list

The -f - will use stdin as the pattern file.
This User Gave Thanks to RudiC For This Post:
# 20  
Old 10-02-2014
Quote:
Originally Posted by RudiC
Well, there might be. Try
Code:
cut -c 1-25 source.list | grep -vxFf - complete.list

The -f - will use stdin as the pattern file.
Thanks.

But I got this error:
Code:
$ cut -c 1-25 source.list | grep -vxFf - complete.list
grep: can't open -


Last edited by Corona688; 10-02-2014 at 10:24 PM..
# 21  
Old 10-04-2014
Maybe my suggestion is outside your main concern on temp files, but you can also try this.
Both input files should be sorted
Code:
$ join -t\@  -v1  incomplete.list source.list

join is a very powerfull tool that helps you process files as databases , provide they are sorted.
This User Gave Thanks to blastit.fr For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

3. Shell Programming and Scripting

[Perl] Same entries in file, but treat them different.

Hi, I could use some help with a program that examines a log file. It should output the duration of the steps in minutes. My problem is that there is no end of a step given, only the begin of a next step. Actually the problem is that this line comes 3 times, but marks 3 different events: ... (6 Replies)
Discussion started by: ejdv
6 Replies

4. Shell Programming and Scripting

to take the output of a command to a file

hi , i am using iostat -nmzx 1 | awk '{ print $4 }' command to get the i/o rates of disks. but i want command output in a file , how can i capture , this is some what difficult because command output is keep on changing , any way i have to get total output of the command . please help me .... (1 Reply)
Discussion started by: shankr3
1 Replies

5. Shell Programming and Scripting

Help me to command to output file format.

Dear Master. Help me to command to out put. Ex log. "<?xml version=""1.0"" encoding=""UTF-10"" ?><anova-test-bom> <txid>17251032659</txid> <authentication> <user>admin</user> <password>Amrduoi</password> </authentication> <destination> <msisdn>1111111</msisdn> ... (2 Replies)
Discussion started by: ooilinlove
2 Replies

6. UNIX for Dummies Questions & Answers

getting Output of ls command in a file

Hi I am trying to get the output of ls into a file with names seaparated with comma and no spaces. Here is the content of my dir: ls file1 file2 file3 file4this is what I tried: ls -m > list.txtthe file 'list.txt' looks like: file1, file2, file3, file4, list.txtI... (25 Replies)
Discussion started by: jdhahbi
25 Replies

7. Shell Programming and Scripting

Redirecting output of a command to a file

Hi We are having a requirement where one shell script, say a.sh (which uses Java and connects to Oracle database using JDBC) keeps on running everytime. I created a wrapper (to check whether a.sh is running and if not then to start it) and scheduled it in the crontab. Now all the output from... (3 Replies)
Discussion started by: ankitgoel
3 Replies

8. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

9. UNIX for Dummies Questions & Answers

Output of command to 2 separate file?

How would I echo the same results to two files? One is a running log, and the other is a cache, or sort. echo "Hello World" >> /Logs/File1 & /tmp/file2 I would just copy it from one place to the other, but the Log keeps history, where I want the /tmp to hold only stuff per session. I tried... (2 Replies)
Discussion started by: TheCrunge
2 Replies

10. Shell Programming and Scripting

awk - treat multiple delimiters as one

Is there anyway to get awk to treat multiple delimiters as one? Particularly spaces... (6 Replies)
Discussion started by: peter.herlihy
6 Replies
Login or Register to Ask a Question