Bash remove first and last characters from a string

1
I

have a file of that looks as follows:

foo bar
tom jerry
UNIX Linux

Each word and/or Linux is a different length. How do strip or remove the last character from each line using bash or ksh shell only on a Linux or Unix-like systems?

You can use any one of the following commands:

Bash/ksh shell substitutioncut commandhead commandtail command

Bash/ksh shell substitution example

The syntax to remove last character from line or word is as follows:

x="foo bar" echo "${x%?}"

Sample outputs:

foo ba

The % is bash parameter substitution operators which remove from shortest rear (end) pattern. You can use the bash while loop as follows:

#!/bin/bash while IFS= read -r line do echo "${line%?}" # or put updated line to a new file #echo "${line%?}" >> /tmp/newfile done < "/path/to/file"

cut command example

The syntax is as follows:

## if length of STRING is 3, pass 2 as character positions echo...

0 0
2

From the following article, you’ll learn how to remove first characters from the beginning of each line in Bash and how to print strings between first and last characters using cut command.

You’ll also learn how to remove last characters of each line using trick with reverse command.

I’ve created a file with the following content and I’ll use it in examples below. So lets say we need to remove first and last characters of each line from the given file.

$ cat file 12345===I Love Bash===54321 12345===I Love Bash===54321 12345===I Love Bash===54321

Remove First N Characters Of Each Line

Use the following command to delete first 5 characters of each line (trim first 5 characters and print each line starting from the 6th character):

$ cat file | cut -c 6- ===I Love Bash===54321 ===I Love Bash===54321 ===I Love Bash===54321

Print Strings Between First and Last Characters

Use the following command to print strings between 9th and 20th characters...

0 0
3

I have a string like that:

|abcdefg|

And i want to get a new string called in someway (like string2) with the original string without the two | at the start and at the end of it

so that i will have this

abcdefg

is that possible in bash?

Answers

You can do

string="|abcdefg|" string2=${string#"|"} string2=${string2%"|"} echo $string2

Or if your string length is constant, you can do

string="|abcdefg|" string2=${string:1:7} echo $string2

Also, this should work

echo "|abcdefg|" | cut -d "|" -f 2

Also this

echo "|abcdefg|" | sed 's/^|\(.*\)|$/\1/'

Here's a solution that is independent of the length of the string (bash):

string="|abcdefg|" echo "${string:1:${#string}-2}"

Going off a few posts listed here it seems the simplest way to do it is:

string="|abcdefg|" echo ${string:1:-1}

edit: works on ubuntu with bash 4.2; does not work on centOS with bash 4.1

You can also use sed to remove the | not just...

0 0
4

The most portable, and shortest, answer is almost certainly:

${t%?}

This works in bash, sh, ash, dash, busybox/ash, zsh, ksh, etc.

It works by using old-school shell parameter expansion. Specifically, the % specifies to remove the smallest matching suffix of parameter t that matches the glob pattern ? (ie: any character).

See "Remove Smallest Suffix Pattern" here for a (much) more detailed explanation and more background. Also see the docs for your shell (eg: man bash) under "parameter expansion".

As a side note, if you wanted to remove the first character instead, you would use ${t#?}, since # matches from the front of the string (prefix) instead of the back (suffix).

Also worth noting is that both % and # have %% and ## versions, which match the longest version of the given pattern instead of the shortest. Both ${t%%?} and ${t##?} would do the same as their single operator in this case, though (so don't add the useless extra character)....

0 0
5

I would like to change all occurrences like “waaa”, “wwwaaaaa”, “wa” by trimming all the “a” in the end.

waaa > w
wwa > ww
waaaaa > w

I know how to find these strings in a text using regular expressions:

grep -nE "wa+" file.txt

I also know how to change one string in a bash script

#!/bin/bash s1="wwwwaaaaaaaa" s2=${s1%w*} echo "$s1 --> $s2"

(modified script from https://stackoverflow.com/a/27658717/5219155)

But I would like to use someting like

sed -E 's/wa+/ZZZ/' file.txt

where ZZZ is something that I need here.

sed -E 's/wa+/${$1%w*}/' file.txt

doesn’t work.

Sample input:

Lorem ipsum wa waaaaaaa saaa dolor sit amet, consectetur aw awwwwaaaa adipiscing elit

Desirable output:

Lorem ipsum w w saaa dolor sit amet, consectetur aw awwww adipiscing...
0 0
6

I would like to change all occurrences like "waaa", "wwwaaaaa", "wa" by trimming all the "a" in the end.

waaa > w
wwa > ww
waaaaa > w

I know how to find these strings in a text using regular expressions:

grep -nE "wa+" file.txt

I also know how to change one string in a bash script

#!/bin/bash s1="wwwwaaaaaaaa" s2=${s1%w*} echo "$s1 --> $s2"

(modified script from https://stackoverflow.com/a/27658717/5219155)

But I would like to use someting like

sed -E 's/wa+/ZZZ/' file.txt

where ZZZ is something that I need here.

sed -E 's/wa+/${$1%w*}/' file.txt

doesn't work.

Sample input:

Lorem ipsum wa waaaaaaa saaa dolor sit amet, consectetur aw awwwwaaaa adipiscing elit

Desirable output:

Lorem ipsum w w saaa dolor sit amet, consectetur aw awwww adipiscing...
0 0
7
COMPANY_NAME=`cat file.txt | grep "company_name" | cut -d '=' -f 2`

outputs something like this

"Abc Inc";

What I want to do is I want to remove the trailing “;” as well. How can i do that? I am a beginner to bash. Any thoughts or suggestions would be helpful.

BASH: How to avoid displaying the last line in grep?

I’m parsing some information from ps -ef |grep process but it always displays in the output of grep the last line which is the grep itself. How can I get the output of grep without the last line? The

How to remove last character in Bash?

I am having problems removing the last character. I have this command: df -h | awk ‘NR>1 {print$1,$2,$3,$4}’ which yields: directoryname1 40K 0 9.0G directoryname2 90K 5.0M 78G directorynamen 0 62

Remove last character in dbms_output.put_line

How to remove last , character from the output FOR v_rec IN (select COLUMN_NAME,DATA_TYPE from cols where table_name = ‘RFI_ATCH_CHKLST_DTL’) LOOP...

0 0
8

Related Post : Command line bash remove first and last characters from

An A-Z Index of the Bash command line | SS64.com
An A-Z Index of the Bash command line for Linux. alias Create an alias • apropos Search Help manual pages (man -k) apt-get Search for and install software packages ...
Last update Tue, 30 Jan 2018 23:56:00 GMT Read More

How can I remove the first line of a text file using bash
I need to repeatedly remove the first line from a huge text file using a bash script. Right now I am using sed -i -e "1d" $FILE - but it takes around a minute to do ...
Last update Tue, 30 Jan 2018 08:19:00 GMT Read More

How do I parse command line arguments in Bash? - Stack
Preferred Method: Using straight bash without getopt[s] I originally answered the question as the OP asked. This Q/A is getting a lot of attention, so I should also ...
Last update Sat, 27 Jan 2018 00:08:00 GMT Read More

A...

0 0
9
Is there a something I can use in a bash script that will allow me to pipe in a string and have it output that string yet with a specified number of characters from the end of it removed?

EG:

Code:

z="The magic string" echo $z echo $z $(echo $z | ultra-cool-command 3)output:

Code:

The magic string The magic string The magic str look into 'cut':

Code:

#!/bin/sh z="The magic string" echo $z echo $z $(echo $z | cut -c0-13) Last edited by z0mbix; 07-25-2003 at . Thanks, however the length of the string in question will vary, so I'm looking for something where you don't need to specifiy a range, yet just the number of characters it needs to remove from the end of the string.

Unless there is a way to find out the length of the string as well?

Last edited by Gaxus; 07-25-2003 at . echo ${z%???}

That would do it for you. Or, you could do something like this:

echo z | sed...

0 0
10

Q) I have a file with bunch of lines. I want to remove the last character in each line from that file. How can i achieve this in unix or linux environment.

Solution:

1. SED command to remove last character

You can use the sed command to delete the last character from a text. The sed command is

sed s/.$// filename

2. Bash script

The below bash script can be used to remove the last character in a file.

#! /bin/bash while read LINE do echo ${LINE%?} done < filename

3. Using Awk command We can use the built-in functions length and substr of awk command to delete the last character in a text.

awk '{$0=substr($0,1,length($0)-1); print $0}' filename

4. Using rev and cut command We can use the combination of reverse and cut command to remove the last character. The command is shown below:

rev products.txt | cut -c2-...
0 0