How to redirect grep command output to same file


 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications How to redirect grep command output to same file
# 1  
Old 05-22-2010
Question How to redirect grep command output to same file

Hi Everyone,

Can anyone please tell me, how can I redirect the grep command output to same file. I am trying with below command but my original file contains no data after executing the command.

$grep pattern file1 > file1

Kind Regards,
Eswar
# 2  
Old 05-22-2010
Hi.

With grep, you can't.

This (very recent - and spookily similar) thread might help: Redirect overwrites itself
# 3  
Old 05-22-2010
Hi Eshwar,

do it with "sed" rather. The "-i" switch has the "in-place" editing.

Code:
sed -ni '/pattern/p' file

However if something goes wrong, you can backup the file . The "-i" switch has that facility.
Code:
sed -ni.bak '/pattern/p' file

This also create a backup of the original file in addition to doing your job file - file.bak

Regards,
Gaurav.
# 4  
Old 05-23-2010
Gaurav,

In my sun solaries system sed does not have -i option.

Can anyone please suggest any other alternative for this.

Regards
Eswar.
# 5  
Old 05-23-2010
Hi,

Do it with perl then -

Code:
 perl -wlni.bak -e 'print if /pattern/' file

does the same thing as the above sed does.

Regards,
Gaurav.
# 6  
Old 05-23-2010
Quote:
Originally Posted by picheswa
Gaurav,

In my sun solaries system sed does not have -i option.

Can anyone please suggest any other alternative for this.

Regards
Eswar.
Its available with GNU sed only and not with standard distributions of sed.

---------- Post updated at 12:41 PM ---------- Previous update was at 12:41 PM ----------

Quote:
Originally Posted by scottn
Hi.

With grep, you can't.

This (very recent - and spookily similar) thread might help: Redirect overwrites itself
There is a way - non standard but.
Code:
sponge

command
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I redirect output from "find", either to a file or another command?

I'm trying to find out what happened to the rogue game that apt-get told me it installed, so I thought I would find the file. I went to the root and entered: find -name "rog*.*" I get a large number of lines saying my access is denied in various directories. I figure I'll practice my Unix... (14 Replies)
Discussion started by: arghvark
14 Replies

2. Shell Programming and Scripting

Redirect script output to file after grep

i have simple program that generate log file 1 line every sec, i need to do grep for specific record then redirect to another file. #!/bin/bash for i in `seq 1 20`; do echo $i sleep 1 done ./test.sh |egrep "5|10|15" 5 10 15 r ./test.sh... (2 Replies)
Discussion started by: before4
2 Replies

3. Shell Programming and Scripting

how to Redirect the output of telnet command on a terminal to a file ?

(/home/user1)-> more script.sh #!/bin/ksh ( echo open devicename sleep 3; echo user; sleep 2; echo password; sleep 2; echo "/info/dump"; ---------> This needs to redirect to a file .Can be number of pages sleep 2; echo "exit" ) | telnet Please use code tags next time for... (2 Replies)
Discussion started by: necro98
2 Replies

4. Shell Programming and Scripting

redirect time command output to file (cygwin bash)

I have set up a bash script to run a long list of things that I need to time. I would like to redirect the output of time to a file. I have set it up like, echo "Runtimes for servlet 4, 100K structures" > test_times.txt echo "" >> test_times.txt echo "runs where N=10" >> test_times.txt echo... (7 Replies)
Discussion started by: LMHmedchem
7 Replies

5. Shell Programming and Scripting

How to redirect the output of a cvs command to a file as well as the console.

Hi can anyone tell me how to redirect the ouput of a cvs command to a file as well as the console? i tried using cvs add <filename> | tee logFile cvs add <filename> 2>logFile 2>&1 All i could get is only on console or on file. Please help Thanks (2 Replies)
Discussion started by: ankitag2010
2 Replies

6. AIX

Unable to get the full content into a file when I redirect installp command output..

When i use the command to check the preview of the filesets to be installed using CLI # When using this commad 'm able to see all Preview view of the filesets to be installed installp -apgX -d "." all # When I redirected the same output to a file 'm able to see only half the details... (1 Reply)
Discussion started by: Sounddappan
1 Replies

7. Shell Programming and Scripting

Redirect grep output to dynamique fileName and subdirectory

Hi all i'm new in KSH, i want to write a script to grep a logs files and redirecting the result into a relative subdirectory/file.txt that must be created near to each log file my begin script is : find ./logs -type f -name "*.log" -exec grep error {} \; how i can perform that modest... (10 Replies)
Discussion started by: rambler
10 Replies

8. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

9. Shell Programming and Scripting

Redirect grep output into file !!!!!

Hi, I am writing the following code in command prompt it is working fine. grep ',222,' SAPPCO_20080306.CSV_old > SAPPCO_20080306.CSV_new But the command is not working in the Shell Script... ########################################## #!/bin/sh #... (2 Replies)
Discussion started by: hanu_oracle
2 Replies

10. Shell Programming and Scripting

redirect the grep output into the variable

i want to redirect the grep output into the variable but i am not able to get it i tried veri=`grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1` vari=$(grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1) BUT NOT... (1 Reply)
Discussion started by: mail2sant
1 Replies
Login or Register to Ask a Question