what would be sed command to delete all characters in line except first 5 leading ones, using sed?
I've tried going 'backwards' on this (reverted deleting) but it's not most elegant solution.
This might work for you (GNU sed):
echo '1234567890' | sed 's/.//6g'
12345
Or:
echo '1234567890' | cut -c-5
12345
Try this (takes 5 repetitions of 'any' character at the beginning of the line and save this in the first group, then take any number of repetition of any characters, and replace the matched string with the first group):
sed 's/^\(.\{5\}\).*/\1/'
Or the alternative suggested by mouviciel:
sed 's/^\(.....\).*/\1/'
(it is more readable as long as the number of first characters you want does not grow too large)
Related
I have the following lines:
XYZ2342
ABCD1323
KIL9824
And I would like to remove all letters at the beginning, so I would get for the above example:
2342
1323
9824
I have tried this:
echo "ABC12345" | sed 's/[[:alpha:]]*[[:digit:]]//'
2345
But it also removes the first digit, how can I make sed to delete just the letters (note: they are always ASCII).
I am using FreeBSD sed implementation, in case that is relevant.
Could you please try following. We need to substitute everything till first occurrence of digits from starting so we need to use [^0-9] regex here, which means from starting substitute everything till first occurrence of digits with NULL here.
echo "ABC12345" | sed 's/^[^0-9]*//'
If I have a string and I want to use sed such that a colon is inserted after the first 8 characters and then after every 2 characters after it, what would the sed pattern look like and what would the replacement pattern look like?
awk has substr() function, it can solve your problem eaiser:
awk -v m="8" -v n="2"
'{a=substr($0,1,m);
b=substr($0,m+1);
gsub(".{"n"}","&:",b)}$0=a":"b'
You can change the m and n to desired value (error handling was not done in my one-liner). If we do a test:
$ awk -v m="8" -v n="2" '{a=substr($0,1,m);b=substr($0,m+1);gsub(".{"n"}","&:",b)}$0=a":"b'<<<"aaaaaaaaaaaaaaa"
aaaaaaaa:aa:aa:aa:a
This might work for you (GNU sed):
sed 's/../&:/4g' file
Insert a : after the 4th occurrence of 2 characters and then globally throughout the line.
I have a simple text file in below format.
1 12658003Y
2 34345345N
3 34653785Y
4 36452342N
5 86747488Y
6 34634543Y
so on
10 37456338Y
11 33535555Y
12 37456378Y
so on
100 23432434Y
As you can see there are two white spaces after first number.
I'm trying to write SED command to remove the digits before whitespaces. Is there any SED command to remove spaces and number before spaces?
Output file should look like below.
12658003Y
34345345N
34653785Y
36452342N
so on..
Please assist. I'm very new to shell scripting.
sed 's/[0-9]\+\s\+//' infile > outfile
Explanation:
s: we want to use substitution
/: mark start and end of the expression we want to match
[0-9]: match any digit
+: match the previous one or more time
\s: space
+: match the previous one or more time
/: mark start of what we want to change our matches to (which is nothing)
/: some special operators goes after this (we use no such)
infile: the file we want to change
>: pipe stdout to
outfile: where we want to store output
Your sed command would be,
sed 's/.* //g' file
This would remove the first numbers along with the space followed.
Remove leading digits, then following spaces:
sed 's/^[0-9]* *//' file
sed 's/^[0-9]*[ ]*//g' input.txt
I have a text file with id and number. All numbers are expected to be 10 digits long. In the following example only the 3rd row is correct.
36000003326|917668001520
36000003359|919005119822
36000003417|9153914209
36000003508|919454102627
If the number is 12 digits long, the first 2 country digits should be removed. The expected results are as follows:
36000003326|7668001520
36000003359|9005119822
36000003417|9153914209
36000003508|9454102627
The following sed is working but it does not check if the target entry is 10 digits or 12 digits long.
sed 's/|91/|/'
How about this?
sed -r 's/\|..(.{10})/|\1/'
One way using awk:
awk -F"|" 'length($2)>10{$2=substr($2,3);}1' OFS="|" file
This migh work for you (GNU sed):
sed -r 's/[0-9]{2}([0-9]{10})$/\1/' file
I want to get a list of lines in a batch file which are greater than 120 characters length. For this I thought of using sed. I tried but I was not successful. How can i achieve this ?
Is there any other way to get a list other than using sed ??
Thanks..
Another way to do this using awk:
cat file | awk 'length($0) > 120'
You can use grep and its repetition quantifier:
grep '.\{120\}' script.sh
Using sed, you have some alternatives:
sed -e '/.\{120\}/!d'
sed -e '/^.\{,119\}$/d'
sed -ne '/.\{120\}/p'
The first option matches lines that don't have (at least) 120 characters (the ! after the expression is to execute the command on lines that don't match the pattern before it), and deletes them (ie. doesn't print them).
The second option matches lines that from start (^) to end ($) have a total of characters from zero to 119. These lines are also deleted.
The third option is to use the -n flag, which tells sed to not print lines by default, and only print something if we tell it to. In this case, we match lines that have (at least) 120 characters, and use p to print them.