How to swap to words in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to swap to words in a file?
# 1  
Old 08-07-2015
How to swap to words in a file?

Hi,

I have a file with certain lines in it.
i want to swap every occurance of word A with B and every occurrance of word B with A.

Example
file.txt
Code:
Unix is a great OS.
Linux support graphical user intrface.
unix and linux are both same.

Output
file.txt
Code:
Linux is a great OS.
Unix support graphical user intrface.
linux and unix are both same.

I tried with the below command but its not working.

Code:
sed -i 's/\(unix\)\(linux\)/\2\1/gi' file.txt

# 2  
Old 08-07-2015
A few points:-
  • You have a mixture of Linux and linux and your code does not cater for that
  • You are only doing one substitution, and will (if it works) leave you with linux for everything.
  • Linux and Unix are not strictly the same.
Maybe consider either three steps (convert unix to @@@@@@, convert linux to unix and then convert @@@@@@ to linux) or a sed script file to do both updates.



Robin
# 3  
Old 08-07-2015
Try using an intermediate pattern (using GNU sed with the i flag extension):
Code:
sed 's/unix/@@/gi; s/linux/Unix/gi; s/@@/Linux/gi' file

-- edit --
Robin mentioned the same...
# 4  
Old 08-07-2015
Quote:
Originally Posted by Little
Hi,

I have a file with certain lines in it.
i want to swap every occurance of word A with B and every occurrance of word B with A.

Example
file.txt
Code:
Unix is a great OS.
Linux support graphical user intrface.
unix and linux are both same.

Output
file.txt
Code:
Linux is a great OS.
Unix support graphical user intrface.
linux and unix are both same.

I tried with the below command but its not working.

Code:
sed -i 's/\(unix\)\(linux\)/\2\1/gi' file.txt

Hello Little,

Following may help you, it will look for exactly word Linux and change it to Unix and word linux to unix, not sure if you are ok without case sensitive substitute also.
Code:
 awk '{gsub(/Unix/,"@@@@",$0);gsub(/unix/,"@@@",$0)gsub("Linux","Unix",$0);gsub("linux","unix",$0);gsub(/@@@@/,"Linux",$0);gsub(/@@@/,"linux",$0);print}' Input_file

Thanks,
R. Singh
# 5  
Old 08-07-2015
awk with an array:
Code:
awk 'BEGIN {A["linux"]="Unix"; A["unix"]="Linux"} {for (i=1; i<=NF; i++) if ((xi=tolower($i)) in A) $i=A[xi]} {print}' file.txt

Trying to retain the case of the first character, as Ravinder did:
Code:
awk 'BEGIN {A["linux"]="unix"; A["Linux"]="Unix"; A["unix"]="linux"; A["Unix"]="Linux"} {for (i=1; i<=NF; i++) if ($i in A) $i=A[$i]} {print}' file.txt


Last edited by MadeInGermany; 08-07-2015 at 01:04 PM..
# 6  
Old 08-07-2015
Code:
perl -pe 'BEGIN{%os = qw(Unix Linux Linux Unix unix linux linux unix)} s/([Ll]inux|[Uu]nix)/$os{$1}/g'  file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Solaris

Explain the output of swap -s and swap -l

Hi Solaris Folks :), I need to calculate the swap usage on solaris server, please let me understand the output of below swap -s and swap -l commands. $swap -s total: 1774912k bytes allocated + 240616k reserved = 2015528k used, 14542512k available $swap -l swapfile dev swaplo... (6 Replies)
Discussion started by: seenuvasan1985
6 Replies

3. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

4. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

5. Shell Programming and Scripting

Swap words using sed

Hi. I have to swap the first and the third word in all lines of a txt file using sed. Separators between words are: any charachter, except intervall. I hope, you'll understand what I want to do. my english is not so good, sorry for that:) (10 Replies)
Discussion started by: T720
10 Replies

6. Shell Programming and Scripting

Splitting concatenated words in input file with words from the same file

Dear all, I am working with names and I have a large file of names in which some words are written together (upto 4 or 5) and their corresponding single forms are also present in the word-list. An example would make this clear annamarie mariechristine johnsmith johnjoseph smith john smith... (8 Replies)
Discussion started by: gimley
8 Replies

7. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

8. HP-UX

Swap device file and swap sapce

Hi I have an integrity machine rx7620 and rx8640 running hp-ux 11.31. I'm planning to fine tune the system: - I would like to know when does the memory swap space spill over to the device swap space? - And how much % of memory swap utilization should be specified (swap space device... (6 Replies)
Discussion started by: lamoul
6 Replies

9. Shell Programming and Scripting

swap words in a line with sed

Hello. There is something I can not manage : I want to swap the first word with the third one in every line. No file is given the input is read from the keyboard. I know I have to use sed, but it seems this is too complicated for me. Could you help me please ? Thanks, atticus (9 Replies)
Discussion started by: atticus
9 Replies

10. Solaris

Swap config - Mirror swap or not?

Hello and thanks in advance. I have a Sun box with raid 1 on the O/S disks using solaris svm. I want to unmirror my swap partition, and add the slice on the second disk as an additional swap device. This would give me twice as much swap space. I have been warned not to do this by some... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies
Login or Register to Ask a Question