cat file1 > file1


 
Thread Tools Search this Thread
Operating Systems Solaris cat file1 > file1
# 1  
Old 03-23-2010
cat file1 > file1

I have file1 with the contents
Code:
 
this is file1

when i do cat file1 , the output is

Code:
 
this is file1


when i run cat file1 > file1 , I receive the following message
Code:
 
cat: input file1 is output

now when i again do

cat file1 , all the contents are gone.

Can somebody explain what is happening?Smilie
# 2  
Old 03-23-2010
Basically you are overwriting the contents of the source file into the dest file where both the files are same.

Use different filenames
Code:
cat file1 > file2

# 3  
Old 03-23-2010
My question is , if i am overwriting the contents thaen i should see the overwritten contens . Why the file is becoming blank . I know that cat file1 > file2 will do the job
I want to know the logic behind this using concept of input and output streams
# 4  
Old 03-23-2010
The reason is simple: before cat is executed, the shell opens file1, and truncates it. Then cat opens file1 for reading, but it won't find any content as that's already gone.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. Shell Programming and Scripting

If file1 and file2 exist then

HI, I would like a little help on writing a if statement. What i have so far is: #!/bin/bash FILE1=path/to/file1 FILE2=path/to/file2 echo ${FILE1} ${FILE2} if ] then echo file1 and file2 not found else echo FILE ok fi (6 Replies)
Discussion started by: techy1
6 Replies

4. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

cannot run file.sh in file1.sh

Hi All, I got a problem in executing block.sh in w.sh. In particular, I got some files as w.sh, block.sh , blackip.db , b.txt in a chain process as below: w.sh --call --> block.sh -->blackip.db--->b.txt <--iptables Ok, here are code w.sh block.sh blackip.db is a file of ip... (2 Replies)
Discussion started by: testcase
2 Replies

6. Shell Programming and Scripting

grep -f file1 file2

Hi I started to learn bash a week ago. I need filter the strings from the last column of a "file2" that match with a column from an other "file1" file1: chr10100036394-100038350AK077761 chr10100041065-100046547AK032226 chr10100041065-100046547AK016270 chr10100041065-100046547AK078231 ...... (6 Replies)
Discussion started by: geparada88
6 Replies

7. UNIX for Dummies Questions & Answers

cat file1 file2 > file3

file1 has pgap500 500 file2 has bunch of data cat file1 file2 > file3 cp file2 file3.dat then vi pgap500 500 onto 1st line compare file3 and fil3.dat, they are not the same. any idea ? the 1st line, i want to put pg500 xxx ---------- Post updated at 07:35 AM ---------- Previous... (2 Replies)
Discussion started by: tjmannonline
2 Replies

8. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

9. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies

10. Solaris

sort -rfn file1 | more

Once again - don't multipost homework questions - read the Rules. (0 Replies)
Discussion started by: wickbc
0 Replies
Login or Register to Ask a Question