Shell command to extract string within bracket (String) in a variable like status(running) [closed] - string

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am building a AIX bash shell utility whereby i get a dynamic variable with value like status(running).
I just need the string within brakets that is running.
Right now i am able to get the whole word with status along with the brackets using awk print.
Can anyone suggest me how to just extract the running out of it. Thanks.

Let's say:
s='(running)'
Using pure BASH:
echo "${s//[()]/}"
running
Using sed:
echo "$s" | sed 's/[()]//g'
running
Using tr:
tr -d '()' <<< "$s"
running
UPDATE: As per comments by OP:
s='status(running)'
Using sed:
echo "$s" | sed 's/^.*(\(.*\)).*$/\1/g'
running
Using pure BASH:
t="${s#*\(}"
echo "${t%)*}"
running

Related

What does grep -v _1_ <file_name> do? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I'm new to bash scripting, can someone tell me what is the meaning of the below command.
grep -v _1_ <file_name> > <new_file>
The -v switch inverts the criteria, so it shows only the lines which does NOT contain the string _1_ and logs the output into the file <new_file>.
The question is wrong, you have written the command as:
grep -v _1_ <file_name> > <new_file>
While it should be:
grep -v _1_ <file1> > <file2>
You can use grep on more file than one, which means that you are looking for something in more than one file.
The -v part is already explained by Antonio.

Convert Scientific Notation into decimal number using bash [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I need to convert 1.22661727081984E+002 to 122.661727081984 using shell script. I have tried it using the below command:
echo "+1.22661727081984E+002" | awk -F"E" 'BEGIN{OFMT="%10.10f"} {print $1 * (10 ^ $2)}'
output:
122.6617270820
It is giving me the output but rounding off the last 4 digits.
I need the output without rounding off the result.
Using Bash's printf:
$ printf "%.14f\n" 1.22661727081984E+002
122.66172708198400
Using awk you can do it on this way:
awk '{ printf("%.100f\n",$1) }'
(assuming the number is first parameter)
Example:
echo "+1.22661727081984E+002"|awk '{ printf("%.100f\n",$1) }'
122.6617270819839973228226881474256515502930000000000000000000000000000000000000000000000000000000000000
(you can limit the number of digits by change the number in front of f)

Change the path address in a text file by shell scripting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my Bash script, I have to change a name to a path address(new address) in a text file:
(MYADDREES) change to ( /home/run1/c1 ) and save it as new file.
I did like this: defined a new variable = new address and tried to replace it in previous address in text file.
I use sed but it has problem.
My script was:
#!/bin/bash
# To debug
set -x
x=`pwd`
echo $x
sed "s/MYADDRESS/$x/g" < sample1.txt > new.txt
exit
The output of pwd is likely to contain / characters, making your sed expression look something like s/MYADDRESS//home/user/somewhere/. This makes it impossible for sed to sort out what should be replaced with what. There are two solutions:
Use a different delimiter for sed:
sed "s,MYADDRESS,$x,g" < sample1.txt > new.txt
...although this will have the same problem if the current path contains a comma character or something else that is a special character for sed, so the more robust approach is to use awk instead:
awk -v curdir="$(pwd)" '{ gsub("MYADDRESS", curdir); print }' < sample1.txt > new.txt

Rename large number of files in bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a large list of files that I want to rename.
Much like this
So this is what my files look like
something.pcap1
something.pcap10
something.pcap11
something.pcap12
...
something.pcap111
something.pcap1111
essentially I want to rename all of the files so that the numbers get padded with 0's and they are 5 digit numbers.
something.pcap00001
A simple for loop should do the trick (can be script file):
for file in $(ls -1 something.pcap*); do
[[ ${file} =~ ^something.pcap([[:digit:]]*).* ]]
newfile=$(printf "something.pcap%05d" ${BASH_REMATCH[1]})
mv ${file} ${newfile}
done
Something like this?
rename 's/\d+$/sprintf("%05d",$&)/e' soemthing.pcap*
Note: this works with the rename as found in debian and its derivates.
What about something like this?
#!/bin/bash
for i in $(ls something.pcap*); do
q=$(echo $i|sed -e 's/pcap/pcap00000/;s/pcap0*\([0-9]\{6,\}\)$/pcap\1/')
mv $i $q
done
I hope this will help

How to use Linux to read a file line by line and replace all the spaces into ','? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am a beginner.. I'd like to use Linux shell to make the following file
1 2 2
2 3 4
4 5 2
4 2 1
....
into
1,2,2
2,3,4
4,5,2
4,2,1
Thank you very much!
Are you looking for something like this:-
sed -e "s/ /,/g" < a.txt
or may be easier like this:
tr ' ' ',' <input >output
or in Vim you can use the Regex:
s/ /,/g
The question asks "line by line". In bash :
while read line; do echo $line | sed 's/ /,/g'; done < file
It will read file line by line into line, print (echo) each line and pipe (|) it to sed which will change spaces into commas. You can add > newfile at the end (but > file won't work) if you need to store it in a file.
But if you don't need anything else than changing characters in the file, processing the whole file at once is easier and probably quicker :
sed -i 's/ /,/g' file
(option -i is for modifying the file directly, as opposed to print modifications to stdout).
Read more about sed to understand its syntax, you'll need it eventually.

Resources