Extract a part of variable/line content in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract a part of variable/line content in a file
# 1  
Old 11-17-2015
Extract a part of variable/line content in a file

I have a variable and assigned the following values
***XYZ_201519_20150929140642_20150929140644_211_0_0_211

I need to read this variable from backward and stop read when I get first underscore (_)

In this scenario I should get 211

Thanks
Kris
# 2  
Old 11-17-2015
Please use code tags as required by forum rules!


bash? Try
Code:
echo ${var##*_}
211

# 3  
Old 11-18-2015
Quote:
Originally Posted by mkris
I have a variable and assigned the following values
***XYZ_201519_20150929140642_20150929140644_211_0_0_211
I need to read this variable from backward and stop read when I get first underscore (_)
In this scenario I should get 211
Thanks
Kris
Hello Kris,

You could try following too.
Code:
echo "***XYZ_201519_20150929140642_20150929140644_211_0_0_211" | awk -F"_" '{print $NF}'

Output will be as follows.
Code:
211

In case you need to read from a Input_file then following may help.
Code:
cat Input_file
***XYZ_201519_20150929140642_20150929140644_211_0_0_211

awk -F"_" '{print $NF}' Input_file

Thanks,
R. Singh
# 4  
Old 11-19-2015
grep

Code:
echo ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 | grep -o "[^_]\+\$"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

2. Programming

PASSING PART OF FILE CONTENT TO VARIABLE

All, I have a log file containing lots of data now i want to extract all text between block below(names) without the title or end pattern but only names, ++++START++++ SCOTT TIGER HENRY PAUL JARED OTIENO OMOLLO JA NIGERIA ++++END++++ the names i want to return and store in a variable in... (1 Reply)
Discussion started by: Scott2000
1 Replies

3. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

4. Shell Programming and Scripting

Using the part of a line as a variable?

Hello Friends, I need a command (or script line) that allows me to use of a part of line (given by me) as a variable. Let us assume the name of the command is MYCMD. When I type MYCMD fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/ it must do the following job cd /homefolder/hhk/ADS/ ... (18 Replies)
Discussion started by: rpf
18 Replies

5. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

6. Shell Programming and Scripting

how to extract a certain part of a line

Hi friends, I want to select and use the certain part of a line. For example I have the following line home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile how can I extract the part " /myhome/afolder/H2O/endfile " thanks (6 Replies)
Discussion started by: rpf
6 Replies

7. Shell Programming and Scripting

file content as a part of an if-statement

Hello, I have following problem. I have the result of a database request. I preparated the result via sed, etc. as a string in a file. The string in the file is: ($3==1 || $3==2 || $3==3 || $3==4) Now I want to use the String as a command in an if-statement. So I assigned the string to a... (3 Replies)
Discussion started by: Dr_Aleman
3 Replies

8. Shell Programming and Scripting

How to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. The name should be set to the date, which is a part of the following line of the xml file: <sceneID>C82_N32_A_SM_strip_008_R_2009-11-24T04:22:12.790028Z</sceneID> How can I separate this line, that the name will... (6 Replies)
Discussion started by: friend
6 Replies

9. Shell Programming and Scripting

how to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. My code looks like this: set name = `awk '/<generationTime>/,/<\/generationTime>/ p' $xml_name` the "name" is thus set to <generationTime>2004-12-01T08:23:50.000000</generationTime> How can I separate this line,... (3 Replies)
Discussion started by: friend
3 Replies

10. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies
Login or Register to Ask a Question