How to replace a string at the end of a line in a text file with another string in UNIX? - linux

I have to replace in a text file the string ".htm" with ".html" if it is placed at the end of line. I should use sed but I don't get how to use it. I tried using grep instead but didn't work.

Use this:
sed 's/\.htm$/.html/' file
It looks for .htm (the dot has to be escaped) whenever it is followed with end of line ($). In that case, it replaces it with .html.
If you want to do an in place edit, add the -i option:
sed -i.bak 's/\.htm$/.html/' file
This will create a backup file.bak while the original will be modified with the new data.
Example
$ cat a
hello this is.htm
hello this is.htm blabla
hello this ishtm
hi!
$ sed 's/\.htm$/.html/' a
hello this is.html
hello this is.htm blabla
hello this ishtm
hi!

or do the following :
1) Open file in vi editor : vi filename
2) go to command mode : press :
3) type the following command and press enter: 1,$s/.htm$/.html/
As suggested in other posts, the same results can be achieved using sed command, but in that case you cannnot make changes in the existing file. Instaed you will have to create a new file. Using the solution above, you can make changes in the existing file.
Hope that helps!!

sed -e 's/\.htm$/\.html/' < foo
assuming foo is the file that you want to operate. output will we standard out, redirect it somewhere useful.

Related

Using 'sed' to replace a string

I have a file that contains a URL and I wish to replace this URL with a new one.
This URL can be different each time so I do not wish to replace XXX with YYY but to change the value of a variable which contains the URL.
File looks like this:
APP_URL=https://test.hello.co/
I wish to change the value of APP_URL to a different URL but without success.
I am using a bash script in order to make this work.
tried using this inside my script and it didn't work.
oldline='APP_URL=https://test.hello.co'
newline='APP_URL=https://${variable}'
sudo sed -i 's/${oldline}/${newline}/g' .env
I would love to get help here!
Thank you :)
You need to remove the ' in sed and escape all / so that it doesn't think you are ending the part of s (or use other sign)
Both these work (my filename is .test):
variable=aaaaaaaaaaaaaaaaaaaaaaaaa
oldline='APP_URL=\S+'
newline='APP_URL=https:\/\/'$variable
sed -r s/${oldline}/${newline}/g .test
oldline='APP_URL=\S+'
newline='APP_URL=https://'$variable
sed -r s-${oldline}-${newline}-g .test
Single quotes prevent variable interpolation.
You can usually use double quotes instead.
$: cat tst
oldurl=test.hello.co
newurl=foo.bar.com
sed "s,APP_URL=https://${oldurl}/,APP_URL=https://${newurl}/,g" file
$: cat file
APP_URL=https://test.hello.co/
$: ./tst
APP_URL=https://foo.bar.com/
edit
Yes, this can be simplified a lot, such as by passing in the new url.
$: grep . tst file
tst:sed -E "s,APP_URL=https://[^/]+/,APP_URL=https://$1/,g" file
file:APP_URL=https://test.hello.co/
$: ./tst a.b.c
APP_URL=https://a.b.c/

OSX Terminal: how to add same item to each line of a csv file?

I have got a csv file like this:
120,256,300
36,255,12
etc...
I want to add a fixed string like 'USA' to all lines in order to obtain:
120,256,300,USA
36,255,12,USA
etc...
How can I do that?
Thanks
From a text processing point of view that CSV file is plain text in this context, you just want to attach , USA to each line.
The easiest (and operationally least expensive) way to do so is probably:
sed -i '' 's/$/, USA/' file
What this does is to instruct sed to look for the end of line $ and "replace" it with , USA. As sed is line-based this obviously doesn't actually trim out the new line of the file.
-i '' instructs sed to make the changes in-line without creating a backup file.
If you wanted a backup you can put the desired extension instead of '', e.g. -i .bak.
You can just use sed: cat <input-file> | sed 's/\(.*\)/\1, USA/'.
Here s is the substitute command, which uses the following character as a separator between a regular expression and a substitution. For the regular expression, the escaped parenthesis are used to create a capture group, the regex .* captures the entire line. For the substitution, the \1 inserts the first capture group, and then the , USA text is appended.
You can perform the replacement in place using: sed -i .bak 's/\(.*\)/\1, USA/' <input-file>

Linux vi File Contents Modifying

Suppose I have these in a file called test:
u001:x:comp111:mark_henry
u002:x:comp321:zack_mike
u003:x:comp132:chris_white
And I want to open that file go to the line that has chris_white and change it to chris_brown so it becomes u003:x:comp132:chris_brown. I'm thinking to use the sed command to do so but I'm not sure how.
Using sed, below method can replace all occurrences of chris_white to chris_brown without opening the file test.
sed -i -e 's/chris_white/chris_brown/g' test
If you want to open the file test in vi editor and replace, then follow the below steps,
1) vi test
2) Type :%s/chris_white/chris_brown/g
3) Press Enter
This will replace all occurrences of chris_white to chris_brown in your file test.
With vi you need more effort than with the basic ed.
echo "s/chris_white/chris_brown/
w
q" | ed -s test
You can use printf for writing the lines as parameters:
printf "%s\n" "s/chris_white/chris_brown/" w q | ed -s test

How to edit string at a line in file in linux

I have file which contains text at line 30 which is:
Icon="\<some path which we do not know\icon.png"
I want to replace above with:
Icon="\home\user\Img\Icons\icon.png"
What is the best way to do it.?
Thanks.
Best way:
perl -pi -e 's/\\<some path which we do not know/\\home\\user\\Img\\Icons/' text.txt
Perl approach is more preferred than sed, because of Unix compatibility.
You can either use an editor to do this manually or if you prefer to do it non interactively, you can use a small shell pipeline and sed.
sed `3 s/big path/custom path/` input_file.txt
where 3 is the line number, big path is what you want to replace and custom path is what you to want to replace it with. input_file.txt is your input file. This will print the replaced file onto the screen which you can redirect into another file using the > operator.
As a concrete example, suppose I have this file (input_file.txt)
Header
Random test
/bad/path/to/some/directory/icon.png
/bad/path/to/some/directory/icon.png
Footer
Now I'm going to run my command like so.
cat input_file.txt | sed '4 s/\/bad\/path\/to\/some\/directory\//\/home\/noufal\//'
and I get
Header
Random test
/bad/path/to/some/directory/icon.png
/home/noufal/icon.png
Footer
Notice that it has changed only the 4th line. The extra \ characters in the command are to escape the / character which has special meaning for sed.
you can use vim to find and replace your strings http://vim.wikia.com/wiki/Search_and_replace or use 'sed' command

How to insert a text in middle of nth line of file

I am trying to insert a pipe symbol in between nth line of a file. Like the one present in the output of diff command.
Do not want to use VI editor.
For example,desired line is 2nd line of the file:
cat filename
Hi Hi
Hello Hello
This This
is Is
it it
desired output:
cat filename
Hi Hi
Hello | Hello
This This
is Is
it it
For your own sanity, just use awk:
$ awk 'NR==2{mid=length($0)/2; $0=substr($0,1,mid) "|" substr($0,mid+1)} 1' file
Hi Hi
Hello | Hello
This This
is Is
it it
To modify the original file you can add > tmp && mv tmp file or use -i inplace if you have GNU awk.
You basically cannot modify in place some textual file by inserting a character inside a line. You need to read all its content, modify that content in memory, then write the new content.
You might be interested in GNU ed, which can edit a file programmatically (inside some script).
You could use awk (or any other filter) and redirect its output to some temporary file, then rename that temporary file as the original one.
sed '
# select 2nd line
/2/ {
# keep original in memory
G;h
:divide
# cycle by taking the 2 char at the egde of string (still string end by the \n here) and copy only first to other part of the separator (\n)
s/\(.\)\(.*\)\(.\)\(\n.*\)/\2\4\1/
# still possible, do it again
t divide
# add last single char if any and remove separator
s/\(.*\)\n\(.*\)/\2\1/
# add original string (with a new line between)
G
# take first half string and end of second string, and place a pipe in the middle in place of other char
s/\(.*\)\n\1\(.*\)/\1|\2/
}' YourFile
posix sed, so --POSIXfor GNU sed
Sel explain

Resources