I have a couple very large files that will not open up in any file editing program. I want to use sed to edit only the first line of headers to take the header and put a descriptor in front of it. My Files are a combination of pipe,comma and tab delimited.
Example:
Name City State Zip
will be ...
ExampleName ExampleCity ExampleState ExampleZip
Like this if you mean the first line of the file:
sed -i '1 s/^.*$/NEW FIRST LINE/' yourfile
Or if the line you mean is not the first, but you only know it starts with "Name":
sed -i '/^Name/s^.*$/NEW HEADER LINE/' yourfile
Added
You can also do this sort of thing
echo Name City State | sed -E 's/([A-Za-z]+)/Example&/g'
Related
let say I have a text file and I want to show echo from it only the 3rd word in the 48th line, how can I do it?
I was able to show a specific line from the text file:
sed -n 48p log.txt
but now I need to find a way to show only the 3rd word...
You can try this:
sed -n 48p log.txt | cut -d' ' -f3
Gets convoluted to do in only sed:
sed -En '48{s/(\s*\S+\s*){2}//;s/\s+.*//;p}'
Delete the first two words, then delete everything except the remaining first word, then print.
I use the command
sed -n "/*/{=;p}" file.txt | sed "{N;s/\n/ /}"
to numbering line.
But, how to save in file or new file?
The file contains
* text one
* text two
* text three
Pipe output of second sed to new file using ">".
Also, there is an nl command in most distros that adds line numbers without needing sed.
I use sed to do a simple replacement to headers in a file.
Sometimes they need to be replaced, sometimes not.
It works fine, but is long because it reads the the files every time (hundreds of MB).
However there is a pattern that separates the header from the content.
How do I tell sed to stop processing the file after encountering a certain pattern ?
Example :
blabla headers that I want to edit here but maybe not FRAME some more content here
Let's say that want to remove "want" from the headers, but the word may or may not be in said headers. I know that I want to stop processing the file at FRAME.
sed -i '0,/\(pattern1\|pattern2\)/s//pattern1/' * ; # TODO stop at FRAME
You can use the q command to quit the sed processing the rest of the input
sed -i '0,/\(pattern1\|pattern2\)/s//pattern1/' * ; /FRAME/q'
/FRAME/ pattern matches the line containing FRAME upon which the command q is excecuted
OR
You can specify an address range from start of the file till it encounters FRAME as
sed '0, /FRAME/ s/old/new'
You can use awk
awk '/pattern stop/ {f=1} !f {sub(/old data/,"new data")} 1' file
This will replace old data with "new data" as long as pattern stop is not found.
To write data back to original file:
awk 'code' file > tmp && mv tmp file
I know a line number in a file, wherein I want to keep the first word and delete the rest till the end of the line. How do I do this using sed ?
So lets say, I want to go to line no 10 in a file, which looks like this -
goodword "blah blah"\
and what i want is
goodword
I have tried this - sed 's/([a-z])./\1/'
But this does it on all the lines in a file. I want it only on one specified line.
If by "first word" you mean "everything up to the first space", and if by "retain this change in the file itself" you mean that you don't mind creating a new file with the same name as the previous file, and if you have a sed that supports -i, you can probably just do:
sed -i '10s/ .*//' input-file
If you want to be more restrictive in the definition of a word, you can use '10s/\([a-z]*\).*/\1/'
Can you use grep or awk to grab just one line, and then pipe it into sed (if grep or awk couldn't do the entire job for you) to work on just one line? I think the key here is isolating that one line first, and then worrying about extracting something from it.
Using awk
awk 'NR==10 {print $1}' file
goodword
The contents of my file are as follows and the desired output is shown below. Using individual sed commands, I am able to modify the file contents. Say
sed -i -e 's!<tag1>FIELD1</tag1>!<tag1>Replaced contents of field1</tag1>! filename
But I am having a tough time trying to replace those individual commands with a single sed script file.
This is a sample file containing a few tags
<tag1>FIELD1</tag1>
<tag2>FIELD2</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
The desired output is
This is a sample file containing a few tags
<tag1>Replaced contents of field1</tag1>
<tag2>Replaced contents of field2</tag2>
<tag2>Some addition to field2</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
You can chain the -e expressions.
For example:
sed -e 's!<tag1>FIELD1</tag1>!<tag1>Replaced contents of field1</tag1>!g' -e 's!<tag2>FIELD2</tag2>!<tag2>Replaced contents of field2</tag2>\n<tag2>Some addition to field2</tag2>!g' filename
tag=( "tag1" "tag2" )
find=( "FIELD1" "FIELD2" )
repl=( "Replacement 1" "Replacement 2" )
regex=
I=$'\x01' # sed delimiter
for (( i=0; i<${#find[#]}; i++ )) ;do
regex+="s$I<${tag[i]}>${find[i]}</${tag[i]}>$I<${tag[i]}>${repl[i]}</${tag[i]}>${I}g;"
done
sed "$regex" "$file"
You may or may not want or need the g at the end of each expression.
Here is what worked for me.
The sed_script file (one single file)
/<tag1>FIELD1<\/tag1>/s!<tag1>FIELD1</tag1>!<tag1>Replaced contents of field1</tag1>!
/<tag2>FIELD2<\/tag2>/c\
<tag2\>Replaced contents of field2</tag2>\
<tag2\>Some addition to field2</tag2>