Bash script to update last character

bash remove last character
bash cut last character
bash remove last character from file
remove last few characters bash
strip last two characters bash
remove the last 4 characters in bash
sed remove last character
bash remove last few characters from string

I have one text file which ends with two kinds of strings, either like 100m, 200m or like 6, 7 etc. When the last string does not contain the "m" character, the bash script should multiply it with 1024 and append with m. No change should happen to the lines ending with m.

I have tried multiple if, else clause with awk but not able to find proper result. Please help. For security reason, I can't share the file but providing a sample below

Input file
fwhjgqdgqdg 100m
gdjegdegge 200m
fdfqwkdgqwdgqwdgh 300m
fhwfgwgdgw1 8998m
tdfdyfdgeu 6
feuegdhe 2
ffhevjevf 0
jhedhevhjkd 0
output should be
fwhjgqdgqdg 100m
gdjegdegge 200m
fdfqwkdgqwdgqwdgh 300m
fhwfgwgdgw1 8998m
tdfdyfdgeu 6124m
feuegdhe 2048m
ffhevjevf 0m
jhedhevhjkd 0m

Try the following -

$ awk '$NF !~ /m/{$NF = ($NF * 1024) "m"} 1' file
fwhjgqdgqdg 100m
gdjegdegge 200m
fdfqwkdgqwdgqwdgh 300m
fhwfgwgdgw1 8998m
tdfdyfdgeu 6144m
feuegdhe 2048m
ffhevjevf 0m
jhedhevhjkd 0m

$NF is the value of the last field on the record (line). If it doesn't contain an m then multiply by 1024 and concatenate an m. The 1 at the end is a true condition which causes awk to invoke it's default action of printing the record.

How to replace the last char of a string in bash with another char , sed -i 's/.$/X/' filename. It will replace last char by X in each line, rewrite file filename . $ cat file | cut -c 1-20 12345===I Love Bash 12345===I Love Bash 12345===I Love Bash Remove Last Character Of Each Line. Using combination of reverse and cut commands we can remove last N characters of each line, as shown below. Use the following command to delete last character of each line in Bash:

Could you please try following.

awk -v var="1024" -v field="2" -v char="m" '$field !~ char{$field= ($field * 1024) char} 1' Input_file

Explanation: I have created variables named var: which will have value which you want to multiply, field you can mention field number here on which field you want to put check, char which character you want to check is present in line or not.

You could change these values as per your need too. Now inside awk command I am checking condition if a line's $field is NOT having var value then do following, which is to multiple field's value with value of variable var and concatenate value of variable char.

Finally mentioing 1 will print edited/non-edited line for Input_file.

Delete the last character of a string using string manipulation in shell , so for example you can delete the last character one character using this: the standard input is read. UPDATE: if you don't know the length of the string, try: is incredibly useful for writing and deciphering shell scripts of many flavors. In bash, how can one remove the last character of a string? In perl, the chop function would remove the last character. However, I do not know how to do the same job in bash. Many thanks in advance. | The UNIX and Linux Forums

Try Perl

perl -pe ' s/(\d+)$/1024*$1/ge and s/$/m/ ' 

with your given inputs

$ cat subit.txt
fwhjgqdgqdg 100m
gdjegdegge 200m
fdfqwkdgqwdgqwdgh 300m
fhwfgwgdgw1 8998m
tdfdyfdgeu 6
feuegdhe 2
ffhevjevf 0
jhedhevhjkd 0

$ perl -pe ' s/(\d+)$/1024*$1/ge and s/$/m/ ' subit.txt
fwhjgqdgqdg 100m
gdjegdegge 200m
fdfqwkdgqwdgqwdgh 300m
fhwfgwgdgw1 8998m
tdfdyfdgeu 6144m
feuegdhe 2048m
ffhevjevf 0m
jhedhevhjkd 0m

$

Remove last character from line, sed 's/.$//'. To remove the last character. But in this specific case, you could also do: df -P | awk 'NR > 1 {print $5+0}'. With the arithmetic expression ( $5+0 ) we� 2. The nano text editor will open. The first step to creating the update script is to add the ‘shebang’ at the top of the file. The ‘shebang’ is simply the path to the program that will execute the script. In this case, Bash will be used.

sed remove last character from each line on Linux / Unix, I need to remove the last character from a line and shell variable such as Here is another small shell script snippet run when a new AWS EC2/Google { _root_ip})" ## Okay, update our sshd config ## ## Disable root login (if� Also see the order of expansions in the bash reference manual. Variables are expanded before the filename expansion. To get the last character you should just use -1 as the index since the negative indices count from the end of the string: echo "${str: -1}" The space after the colon (:) is REQUIRED. This approach will not work without the space.

bash - How can I extract the last X chars from a string variable?, Your shebang line is pointing to bash , but you are running the script with sh , so the shebang is ignored. /bin/sh on Ubuntu systems is actually� If you want to use a special character as a literal (non-special) character, you have to tell the Bash shell. This is called quoting, and there are three ways to do it. If you enclose the text in quotation marks (“…”), this prevents Bash from acting on most of the special characters, and they just print.

How to 'cut' last character in a string in bash shell, There are multiple ways to cut or remove the last character in a string in a bash script. The two that come to my mind first are using sed or using expr with a regex � The above backup.sh script update introduces few new bash scripting techniques but rest for the code between Lines 5 - 13 should be by now self-explanatory. Line 5 is using a -z bash option in combination with conditional if statement to check whether positional parameter $1 contains any value.

Comments
  • edit your question to show what you've tried so far.
  • By seeing your propfile @Subit Das same to know you hardly show your efforts which you have put in order to solve your own problem and you NEVER select an answer as correct one. Kindly try to do both the things for your questions.
  • Please take a look at: What should I do when someone answers my question?
  • I will do so. I was in doubt, whether to put this in stackoverflow or not. After struggling for few hours, I decided to put it. I asked for awk because BC is not supported in my linux environment. I was searching for using if else clause inside awk. I should not have asked direct requirement. I should have asked to understand the concept first like what majorities are doing.
  • almost same - awk '/m$/{print} /[0-9]$/{ print $1 " " $2*1024 "m" }'. I don't like that it makes so many assumptions, but hey, specs is specs, yes? :D
  • I added an explanation to the answer, see the documentation for more background, especially the book Effective Awk Programming, 4th Edition, by Arnold Robbins.
  • Thanks for the answer. I could have tried if my environment supported perl.