Swapping content b/n two files


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Swapping content b/n two files
# 1  
Old 11-17-2016
Swapping content b/n two files

I am having two files file1&file2.i just want to swap the selected contents btwn two files
#file1
content:
Code:
Title:xxxx
Hello Imran

#file2
content:
Code:
Title:YYYY
Hello Meeran

i just want only second line in both files should be swapped.The title should remain in the same file.

i just want the output like this

file1:
Code:
Title:xxxx
Hello Meeran

file2:
Code:
Title:YYYY
Hello Imran


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-17-2016 at 09:13 AM.. Reason: Added CODE tags.
# 2  
Old 11-17-2016
Hello Meeran Rizvi,

Could you please try following and let me know how it goes then.
Code:
awk 'FNR==NR{S[FNR]=$0} FNR==2 && NR==2{Q=$0;;next} FNR==2 && NR>FNR{S[FNR]=$0;$0=Q;print > ARGV[2]"_tmp";next} NR>FNR{print > ARGV[2]"_tmp"} END{for(i=1;i<=FNR;i++){print S[i] > ARGV[1]"_tmp"}}' Input_file1  Input_file2 && mv Input_file1_tmp Input_file1 && mv Input_file2_tmp Input_file2

Above code will only swap 2nd line of Input_files not any other line as per your request.
EDIT: Adding a non-one liner form of solution on same now.
Code:
awk 'FNR==NR{
                S[FNR]=$0
            }
     FNR==2 && NR==2 {
                        Q=$0;
                        next
                     }
     FNR==2 && NR>FNR{
                        S[FNR]=$0;
                        $0=Q;
                        print > ARGV[2]"_tmp";
                        next
                     }
     NR>FNR          {
                        print > ARGV[2]"_tmp"
                     }
     END             {
                        for(i=1;i<=FNR;i++){
                                                print S[i] > ARGV[1]"_tmp"
                                           }
                                           }
    ' Input_file1  Input_file2 && mv Input_file1_tmp  Input_file1 && mv Input_file2_tmp  Input_file2

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-17-2016 at 08:45 AM..
# 3  
Old 11-17-2016
Pure shell:
Code:
{ read L11; read L12; read L21 <&3; read L22 <&3; echo $L11; echo $L22; echo $L21 >&4; echo $L12 >&4; } <file1 3<file2 4>TMP

For longer files, you may add a tail -n+3 for each file.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-18-2016
Hi Rudic,
can u explain me the above code plz.
# 5  
Old 11-18-2016
Code:
{ read L11;                     # from file1 via stdin (fd 0)
  read L12;                     # same
  read L21 <&3;                 # from file2 via redirection (fd 3)
  read L22 <&3;                 # same

  echo $L11;                    # to stdout (fd 1)      (var comes from file1)
  echo $L22;                    # same                  (var comes from file2)
  echo $L21 >&4;                # to redir (fd 4)       (var comes from file2)
  echo $L12 >&4;                # same                  (var comes from file1)

} <file1 3<file2 4>TMP          # redirection for input and to "TMP" file

# 6  
Old 11-18-2016
I tried with tail&sed
its working as expected.
Code:
echo "Enter the file1"
read file1
echo "Enter the file2"
read file2
tail -n+2 $file1 > file3
sed -i '2,$ d' $file1
tail -n+2 $file2 >> $file1
sed -i '2,$ d' $file2
tail -n+1 file3 >> $file2
rm file3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Compare content between two files

I have two files in unix environment with similer type of contain: Example: File1 File2 Milestone1 Milestone1 Milestone2 Milestone12 Milestone3 Milestone13... (11 Replies)
Discussion started by: Mrinal Mondal
11 Replies

3. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

4. Shell Programming and Scripting

Changing and swapping the Values in the files

Hi all we have a file ONE like this 12345 98765 67222 74252 76991 90091 and we have one more file TWO like huiiii 67jjjj u988 99999 uj99j 98765 hujg 7yhh ij999 78688 ijo99 74252 Now i want create THREE file which is like huiiii 67jjjj u988 12345 uj99j 98765 hujg 7yhh ij999... (2 Replies)
Discussion started by: polineni
2 Replies

5. Shell Programming and Scripting

Files appear to have no content

I have about 11000 files on a hard disk that were generated by copying a directory structure from a dvd. The files are supposed to contain ascii data. I am unable to read or inspect the contents of several of the files, I receive the error no such file or directory. I am using cygwin on... (4 Replies)
Discussion started by: jvorwald
4 Replies

6. Shell Programming and Scripting

how to find a content in all files

Hi, i would like to find 'AppManage' in all files. i have tried the following but it didn't work. /local/home/mani>grep -iR 'AppManage' *.* - not outcome /local/home/mani>grep -iR 'AppManage' - this one hangs thanks (3 Replies)
Discussion started by: lookinginfo
3 Replies

7. Shell Programming and Scripting

Help with Combining Content of Two files

I'm trying to combine it using awk but still can't figure it out. Here is the file. cat file1.txt Mr Smith Mr John Ms Linda cat file2.txt No 4 Jln Empat Kuala Lumpur No 213 Tmn Bunga Kedah No 1 Kampung Bukit Malaysia I want to combine this file1 and file2 so the output... (5 Replies)
Discussion started by: pisang
5 Replies

8. UNIX for Dummies Questions & Answers

display files name and it's content

hello, i have a question and i'll be appreciated if you can help me with this. i have i folder let's say "users" in this folder there are files (file1, file2, file2, etc) and in each file there is a number (lets say 4 in file1) i want to display some thing like bellow file1 4... (6 Replies)
Discussion started by: dndoon
6 Replies

9. Shell Programming and Scripting

comparing files content

hi i have a set of files , i need to compare one file content with other file content, i am using cmp -s abc.1 def.2 , but it is not giving theproper o/p even if the content is different.Please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

10. Shell Programming and Scripting

content need changes- many files

Hi, all Need some advise on this script i have some files in a directory, and i need to change the word XX in each files to YY, how can i perform a bulk changes/ thanks in advance for yr sharing (3 Replies)
Discussion started by: swchee
3 Replies
Login or Register to Ask a Question