lower case to uppercase conversion [duplicate] - linux

This question already has answers here:
How to convert a string to lower case in Bash
(29 answers)
Closed 9 years ago.
I found solution but it doesn't working http://www.cyberciti.biz/faq/linux-unix-shell-programming-converting-lowercase-uppercase/
[root#mg0016 tmp]# y="this Is A test"
[root#mg0016 tmp]# echo "${y^^}"
-bash: ${y^^}: bad substitution

You can use any one of the following code :
$ tr '[:lower:]' '[:upper:]' < input.txt > output.txt
or
$ sed -e 's/\(.*\)/\U\1/' input.txt > output.txt

I found following one! and it works!
[spatel#mg0016 ~]$ echo "lower" | awk '{print toupper($0)}'
LOWER
Thanks for reply all.

Related

Parse .env file with bash [duplicate]

This question already has answers here:
How to grep for contents after pattern?
(8 answers)
Closed 4 years ago.
I have .env file and I am trying to parse the value from it.
I ran this
cat .env | grep PORT=
I got
PORT=3333
How do I grab the value of a specific key?
cat env | grep PORT= | cut -d '=' -f2
Let say your input looks like this :
$ cat test.txt
Port=2020
Email=me#myserver.com
Version=2.02
Then this will do :
awk -F'=' '/^Version/ { print $2}' test.txt
Output
2.02
Use eval to parse the assignment line, later variable values can be substituted with $:
eval "$(grep ^PORT= .env)"
echo $PORT

Unable to use sed to replace text with shell variable [duplicate]

This question already has answers here:
Environment variable substitution in sed
(12 answers)
Closed 5 years ago.
For some reason, the answer in the post below doesn't work for me. Any thoughts?
how to use sed to replace a string in a file with a shell variable
I'm running CentOS 6.5
`NEW="new value"
cat myfile.txt | sed -e 's/old/${NEW}' <-- just replaces 'old' with '${NEW}'
cat myfile.txt | sed -e 's/old/$NEW' <-- just replaces 'old' with '$NEW'
cat myfile.txt | sed -e "s/old/${NEW}" <-- gives the error: unknown option to `s'
try taking the 's off the sed e.g
$ new=N
$ cat > j
one
two
three
$ sed -e "s/one/${new}/" j
N
two
three
for a more complete answer try this answer

Linux operating system [duplicate]

This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 5 years ago.
I am a beginner at Linux and I'm trying to do a project which takes every line from a file.txt and replaces the third word with the first of each line. Here is my Shell code but it doesn't seem to work. It keeps replacing the third word with $field1 and not what's in it.
#!/bin/bash
while IFS=: read -r field1;do
sed -e 's/[^:]*[^:]/$field1/3'
done < file.txt
Try this, this will replace in the same file:
#!/bin/sh
while read -r line
do
first=`echo $line | awk -F':' '{ print $1 }'`
last=`echo $line | awk -F':' '{ print $3 }'`
echo $line | sed "s/$last/$first/"
done < file.txt
Input file :
ashish:is:good
navin:is:good
how:are:you
Output :
ashish:is:ashish
navin:is:navin
how:are:how
Make note of the single quotation marks. Place them around the field1 variable and so:
sed -e 's/[^:]*[^:]/'$field1'/3'

How to remove "-" and a space from the beginning in a bash script? [duplicate]

This question already has answers here:
Editing/Replacing content in multiple files in Unix AIX without opening it
(2 answers)
Closed 6 years ago.
I have an output that looks as below
- 0.1-1
- 0.1-2
- 0.1-3
- 0.1-6
- 0.1-7
- 0.1-9
How to use grep or something else so as to remove "-" and a space from the beginning.
0.1-1
0.1-2
0.1-3
0.1-6
0.1-7
0.1-9
With sed:
sed -e 's/^- //' input.txt
Or with GNU grep:
grep -oP '^- \K.*' input.txt
You may use grep also,
grep -oE '[0-9].*' file
With awk:
awk '{print $2}' file
You can use cut to remove the first two columns of every line:
cut -c3- input.txt

Extract text between two given different delimiters in a given text in bash [duplicate]

This question already has answers here:
Print text between delimiters using sed
(2 answers)
Closed 2 years ago.
I have a line of text which looks like hh^ay-pau+h#ow, I want to extract the text between - and + which in this case is pau. This should be done in bash. Any help would be appreciated.
EDIT: I want to extract the text between the first occurence of the tokens
PS: My google search didn't take me anywhere. I apologize if this question is already asked.
The way to do this in pure bash, is by using parameter expansions in bash
$ a=hh^ay-pau+h#ow
$ b=${a%%+*}
$ c=${b#*-}
$ echo $c
pau
b: remove everything including and behind the first + occurence
c: remove everything excluding and before the first - ocurrence
More info about substring removing in bash parameter expansion
Try
grep -Po "(?<=\-).*?(?=\+)"
For example,
echo "hh^ay-pau+h#ow" | grep -Po "(?<=\-).*?(?=\+)"
If you have only one occurence of - and + you can use cut:
$ echo "hh^ay-pau+h#ow" | cut -d "-" -f 2 | cut -d "+" -f 1
pau
Assuming one occurence of + and -, you can stick to bash
IFS=+- read -r _ x _ <<<'hh^ay-pau+h#ow'
echo $x
pau
If you're guarenteed to only have one - and one + .
% echo "hh^ay-pau+h#ow" | sed -e 's/.*-//' -e 's/+.*//'
pau
echo "hh^ay-pau+h#ow" | awk -F'-' '{print $2}' |awk -F'+' '{print $1}'

Resources