Saving a certain string from command line [duplicate] - linux

This question already has an answer here:
how to print certain column with numbers only in awk
(1 answer)
Closed 5 years ago.
i am writing a bash script, and when i execute a certain command from my script it spits out an ID like this
VM ID: 12345
IDs are different all the time. How would I be able to extract just the number of ID and store it in my script?
I tried to put a ">file" after the command and it does not seem to work.

So, it depends on the possible IDs.
If it is always the 3rd item in string.
echo "VM ID: 12345" | awk '{print $3}' > file
If the output is always the only numbers in the output you can use tr.
echo "VM ID: 12345" | tr -d '[:alpha:][:blank:][:punct:]'
12345
However, if another number is in the string it will be added.
echo "VM3 ID: 12345" | tr -d '[:alpha:][:blank:][:punct:]'
312345
You can also make a pattern that gets the number after the first ":"

Related

Print new line as string literal in unix or shell [duplicate]

This question already has answers here:
How to replace one character with two characters using tr
(5 answers)
Closed 3 years ago.
Hi I have a shell script that has
variable="apple banana monkey"
I want it to be
apple\nbanana\nmonkey
But when I try and execute
echo $variable | tr ' ' '\n'
It results to
apple
banana
monkey
I want to get the actual literal of new line and not the evaluated value.
I have tried echo -e or echo -n or even put numerous escapes \\ but to no avail.
Please help. Thanks
tr command translates chars into chars by performing a 1 to 1 mapping. You are asking the tool to translate a space into two chars, which is something that cannot be done with tr.
If you accept a command switch, you can try with sed:
echo "$variable" | sed 's/ /\\n/g'

How can I extract a specific number from df in bash? [duplicate]

This question already has answers here:
How can I *only* get the number of bytes available on a disk in bash?
(7 answers)
Closed 6 years ago.
My aim is checking if there is still enough space on my disk, every time my script (bash) proceeds a step.
Running df; echo $? prints:
Dateisystem 1K-Blöcke Benutzt Verfügbar Verw% Eingehängt auf
/dev/sdc4 1869858440 1680951776 93900284 95% /mnt/dd
0
The 0 is the result of that command.
In my case, I only want 93900284 in a variable or as the result.
I already read man df.
df --output=avail /path/to/where/you/want/to/write | tail -n 1
BTW: bash 'returns' (in this case 0 == success) are exit codes, the way you phrase it it seems you try to capture that rather than the output. In that case, you might want to read this.
You can use awk to extract suitable field from output:
BASH_VAR=`df | awk '/\/dev\/sda4/{print $4;}''`
If what you want to do is display only available disk space, you can use the following command
df -k /dev/sdc4 | tail -1 | awk '{print $4}'

Read first characters of a variable in bash script? [duplicate]

This question already has answers here:
In Bash, how can I check if a string begins with some value?
(13 answers)
Closed 6 years ago.
I have a file where i get some informations through a bash script to put data in a DB table and i'd like to know how to read the first characters of a variable because if it starts with "CE-" that line's data will go into a table if not they must be inserted in an other one, how can i do this?
Like this-
var=CE-xxxxx
echo "$var"
output- CE-xxxxx
var2=$(echo "$var" | cut -c 1-3)
echo "$var2"
output- CE-
Then you can check if $var2 matches your criteria and use it further.
You can use cut to get the bytes that you need:
V="CE-IMPORTANT"
I=$(echo $V | cut -b 4-)
If you want to use the - as separator:
I=$(echo $V | cut -d '-' -f 2)
In both cases you get "IMPORTANT" in I var

ubuntu terminal: grep for numbers compare [duplicate]

This question already has answers here:
Is it possible to use egrep to match numbers within a range?
(2 answers)
Closed 6 years ago.
I have text file with table |ID | NAME | CREDIT| and content
Is it real to get all lines, where CREDIT < 1337(for example) by grep and ONLY with GREP, no awk or something else?
Have no idea, tnx
You can do it with pure grep, but it's ugly. Here you are:
grep -e " .$" -e " ..$" -e " ...$" -e " 1[0-2]..$" -e " 13[0-2].$" -e " 133[0-6]$"
This is a job very much unsuited to grep. As an artisan, you should select your tools carefully, no-one wants to try cutting down a giant Karri tree with a screwdriver :-)
It is almost certainly a job for awk. You haven't specified your content lines so let's assume for now they're of the form:
|iii|nnnnnnn|ccccc|
where the i, n and c sequences are the relevant column data.
To get those lines where the credit value is less than 1337, it's a simple matter to do:
awk -F'|' '$4 < 1337 {print}' inputFileName

unix bash string search without using awk or sed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can't use awk or sed or any other string processing utilities
Any help on this problem is appreciated I have a text file with the following format
BobJanitor20000
TedBuilder30000
NedFighter25000
KitTeacher40000
yes, assume that the names are always three characters long, profession 7 characters and salary 5 characters edit: varaibles come from user input and not as a parameters
I ask the user for a name input and another input whether to display occupation or Salary
If the user enters "Ted" and chooses salary, the output should be
Ted 30000
The program must also take into account partial name matches, "ed" and salary should output
Ted 30000
Ned 25000
I know cut and grep can get me the relavent lines but how do i create the output i want?
cut -c1-3 textFile| grep "$user_input"
gets me the lines i want to use but how do i isolate the Name and profession columns and then the name and salary columns
You must split the input lines in fields first. Something like grep "$user_input" textFile will fail when the input matches a part of the job.
For this reason a simple approach with grep will fail:
With grep you have the option -o to show the matching part only. Combine this with a dot for a single character, and ^ for the beginning of a line or a $ for the end of the line.
# Show salary
# echo "TedBuilder30000" | grep "Ted" | grep -o ".....$"
# Show job
# echo "TedBuilder30000" | grep "Ted" | grep -o "............$" | grep -o "^......."
This will become messy when you want to show the matched name (Ted/Ned) as well.
So how do we split everything up?
I already stored the userinput for name and display in variables. The display is converted in lowercase automaticly with the typeset -l.
userinput=ed
typeset -l display
display=Salary
while read line; do
# offset 0, length 3
name=${line:0:3}
job=${line:3:7}
sal=${line:10:5}
# echo "Debug: Name=$name Job=$job Sal=$sal"
# double brackets for matching the userinput with wildcards
if [[ "$name" = *${userinput}* ]]; then
# Use case for a switch between different possible values
case "${display}" in
"occupation|job") echo "${name} ${job}";;
"salary") echo "${name} ${sal}";;
*) echo "Unsupported display ${display}";;
esac
fi
done < testFile # While reads from testFile, I avoid using cat (Google for uuoc)
You may want:
grep "$user_input" textFile | tee >(cut -c '11-15') | echo `cut -c 1-2`
This is called process substitution.
See https://unix.stackexchange.com/questions/28503/how-can-i-send-stdout-to-multiple-commands
You can use cut with character ranges, for example
echo NedFighter25000 | cut -c 1-3

Resources