Friday 22 August 2014

UNIX: Replacing the contents of a File

Replacing the contents of a File:
FOLD:
This command is used to create new lines after particular length in a file which is of stream format (ie without new line)

fold -w length path/filename1.txt > path/filename2.txt

fold -w 50 path/filename1.txt > path/filename2.txt 

This command creates new line after every 50 character lengths.
SED:
For deleting rows/lines containing a particular character/word, sed command can be used.
sed '/04546986/d' path/filename1.txt > path/filename2.txt

This command will delete the lines which has 04546986
AWK:
To replace a particular string/character with new line, we go for awk command.

awk '{gsub("|","\n"); print}' Path/filename1.txt > Path/FileName2.txt

This command will remove the PIPE symbol | from the file and replace it with new line.
SED:
To replace a particular string/character with another string/character, we go for sed command.
sed -e 's/YYY/XXX/g' Path/Filename1.txt > Path/Filename2.txt


This Command will replace YYY in the file Filename1.txt with XXX and writes it to Filename2.txt 

No comments:

Post a Comment