Delete first and last character from from each line of a txt file - shell

I need to delete the first and the last character from the each line of a text file.
for example:
Input
$cat file1.txt
|head1|head2|head3|
|1|2|3|
|2|3|4|
Output:
head1|head2|head3
1|2|3
2|3|4

Using sed:
sed 's/.$//; s/^.//' inputfile

A simple way to do it in one sed command:
sed -E 's/^.|.$//g' file
Match a character at the start or the end of the line and replace with nothing.
In basic mode, remember that the | needs to be escaped:
sed 's/^.\|.$//g' file

If awk is helpful:
awk '{print substr($0,2,length($0)-2)}' file
head1|head2|head3
1|2|3
2|3|4

#smisra- Could you please try following, it may help you in same too.
awk '{gsub(/^\||\|$/,X,$0);print}' Input_file

Related

Find multiple strings between values and replace with newline in bash

I need to write a bash script to list values from an sql database.
I've got so far but now I need to get the rest of the way.
The string so far is
10.255.200.0/24";i:1;s:15:"10.255.207.0/24";i:2;s:14:"192.168.0.0/21
I now need to delete everything between the speech marks and send it to a new line.
desired output:
10.255.200.0/24
10.255.207.0/24
192.168.0.0/21
any help would be greatly appreciated.
$ tr '"' '\n' <<< $string | awk 'NR%2'
10.255.200.0/24
10.255.207.0/24
192.168.0.0/21
You could use :
echo 'INPUT STRING HERE' | sed $'s/"[^"]*"/\\\n/g'
Explanation :
sed 's/<PATTERN1>/<PATTERN2/g' : we substitute every occurrence of PATTERN1 by PATTERN2
[^"]*: any character that is not a ", any number of time
\\\n: syntax for newline in sed (reference here)
Considering that your Input_file is same as shown sample then could you please try following.
awk '
{
while(match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+/)){
print substr($0,RSTART,RLENGTH)
$0=substr($0,RSTART+RLENGTH)
}
}' Input_file
This might work for you (GNU sed):
sed 's/"[^"]*"/\n/g' file
Or using along side Bash:
sed $'/"[^"]*"/\\n/g' file
Or using most other sed's:
sed ':a;/"[^"]*"\(.*\)\(.\|$\)/{G;s//\2\1/;ba}' file
This uses the feature that an unadulterated hold space contains a newline.

sed delete all occurences of pattern in each line

The following command doesn't repeat the process for each occurence in one line...
input_file.txt :
<!--:nl-->hond <span>bob</span><!--:fr-->chien <span>bob</span><!--:nl-->kat<!--:fr-->chat
<!--:nl-->hond<!--:fr-->chien<!--:nl-->kat<!--:fr-->chat
wrong sed command :
sed -e 's/\(\<\!--\:nl\--\>\).*\(\<\!--\:fr\--\>\)/\1\2/g' input_file.txt > output_file.txt
current output_file.txt result :
<!--:nl--><!--:fr-->chat
desired output_file.txt result :
chien <span>bob</span>chat
chienchat
[EDIT] hond, chien, kat and chat may have HTML tags around them that need to be kept...
You can use this sed:
sed 's/<!--:nl-->[^<]*<!--:fr-->//g' file
Following awk may also help you in same.
awk -F'<!--:nl-->|<!--:fr-->' '{print $3$5}' Input_file
Explanation: Simply making strings <!--:nl--> OR <!--:fr--> as field separators and then printing 3rd and 5th columns of the line(as per your output required).

replace a string before the semi colon

I have several files, which begins like this :
unit,s_adj,partner,stk_flow,indic,geo\time;aaaa;2222;
time,s_adj,partner,stk_flow,lolo,geo\time;bbb;2222;
I want to replace the first occurence before the semi-colon with that new occurence YEAR
The desired output would be:
YEAR;aaaa;2222;
YEAR;bbb;2222;
I tried with the following command line but it does not seem to do what I want
awk -F ";" 'NR==1 {$1=""; print "year"}' input_file
Your suggestions are welcomed.
Best.
try this:
sed 's/[^;]*/YEAR/' file
if you only want the substitution happen on the 1st line:
sed '1s/[^;]*/YEAR/' file
You can also do:
awk '{$1="YEAR"}1' OFS=\; FS=\; input-file

insert a blank line between every two lines in a file using shell, sed or awk

I have a file with many lines. I want to insert a blank line between each two lines
for example
original file
xfdljflsad
fjdiaopqqq
dioapfdja;
I want to make it as:
xfdljflsad
fjdiaopqqq
dioapfdja;
how to achieve this?
I want to use shell script, awk or sed for this?
thanks!
With sed, use
sed G input-file
If pilcrow is correct and you do not want an additional newline at the end of the file,
then do:
sed '$!G' input-file
Another alternative is to use pr:
pr -dt input-file
awk '{print nl $0; nl="\n"}' file
My approach if I want to quickly regex a file.
vim file.txt
%s/\n/\n\n/g
Idiomatic awk:
awk 1 ORS='\n\n' file
Similar thing with perl:
perl -nE 'say' file
Append | head -n -1 if final newline is unwanted.

How to extract specific lines from a file in bash?

I want to extract the string from a line which starts with a specific pattern from a file in shell script.
For example: I want the strings from lines that start with hello:
hi to_RAm
hello to_Hari
hello to_kumar
bye to_lilly
output should be
to_Hari
to_kumar
Can anyone help me?
sed is the most appropriate tool:
sed -n 's/^hello //p'
Use grep:
grep ^hello file | awk '{print $2}'
^ is to match lines that starts with "hello". This is assuming you want to print the second word.
If you want to print all words except the first then:
grep ^hello file | awk '{$1=""; print $0}'
You could use GNU grep's perl-compatible regexes and use a lookbehind:
grep -oP '(?<=hello ).*'

Resources