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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Printing a part of the last line of the specific part of a file
# 1  
Old 09-03-2012
[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:

Code:
Name =A
xxxxxx
yyyyyy
zzzzzz
aaaaaa
bbbbbb
Value = 57

This is necessary because in a file there are written more lines which contains a string "Value =[...]"

Next, I want to get the value 57 and assign it to a variable (this is an example because each file has different value)

I tried to use this command:

Code:
VALUE=`sed -n '/Name = A/,/Value =/p' $file | tail -1 | gawk '{printf $3"\n"}'`

Unfortunately, this command runs very slow for 80 files.

I tried also to use this command:

Code:
VALUE=`sed -n '/Name = A/{n;n;n;n;n;n;s/\(.*\)= //;p;}'`

However, I'd rather not define the amount of lines which should be skipped because it is possible to insert a newline in that file and in this case the script won't work.

Can anyone help me with this?
Thanks
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 09-03-2012 at 11:05 AM.. Reason: code tags, please!
# 2  
Old 09-03-2012
Code:
awk -F= '$1 ~ "Name" {n=$2;next} n && $1~"Value"{print n,$2;n=""} OFS== myFile

# 3  
Old 09-03-2012
Thank you for your quick reply

However, there is unmatched ' , wherever I try to put the second ' i receive the output which says that there is an syntax error. Could you please help me with this?
# 4  
Old 09-03-2012
Code:
sed -n -r '/Name =A/,/Value =/{/Value = (.*)/ s//\1/p}' $file

This User Gave Thanks to delugeag For This Post:
# 5  
Old 09-03-2012
Code:
awk 'BEGIN{RS="Name";} {for(i=1;i<=NF;i++){if($i=="Value"){i+=2;print $i;break;}}}' input_file

This User Gave Thanks to msabhi For This Post:
# 6  
Old 09-03-2012
Quote:
Originally Posted by vgersh99
Code:
awk -F= '$1 ~ "Name" {n=$2;next} n && $1~"Value"{print n,$2;n=""}' OFS== myFile

sorryy, fat fingers...
This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 09-04-2012
Thank you very much, now it works fine
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

How to print the specific part of the file name with file creation date?

Hello Folks, I have an requirement, where i need to get total count of the file based on creation date with there filename selected pattern. Filename: MobileProtocol.20171228T154200.157115.udr I want to get the count of files created on each day based on a pattern find. find . -type... (7 Replies)
Discussion started by: sadique.manzar
7 Replies

3. Shell Programming and Scripting

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 (3 Replies)
Discussion started by: mkris
3 Replies

4. UNIX for Dummies Questions & Answers

Grep a part of a line from a file

Hi, I have a file with thousands of lines as below INSERT INTO T_DIM_CLNT(CLNT_KY,CLNT_OBJ_ID,ISI_CLNT_ID,OPERN_ID,CLNT_NM,PRMRY_SIC_CD,PRMRY_SIC_DSC,RET_AGE_NBR,REC_CRT_TS,REC_DATA_EXTRC_TS,ETL_LOG_KY) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)... (5 Replies)
Discussion started by: sudhakar T
5 Replies

5. 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

6. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

7. Shell Programming and Scripting

Change in last line of each part of a file

Hi, I have a input files as below. Sample of one input file type mtg_key = record decimal("\344") id; string("\344") ct_cd; string("\344") st_cd; end; type psl_key = record decimal("\344") id; utf8 string("\344") st_cd; end; type amk_fields = record ... (6 Replies)
Discussion started by: adgangwar
6 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Rename file name / remove part of name

I have a whole file structure with jpeg files where I want to remove a part of the file name. An application added in many files a case conflict in the naming "xyz 017.jpg (Case Conflict 1)" So, can someone help me how to get rid of the " (Case Conflict 1)"? What I have is this: find . -name... (2 Replies)
Discussion started by: borobudur
2 Replies

9. UNIX for Dummies Questions & Answers

Printing a part of a string

hi I have the input as follows ABC =893 JKL = "jee" alias PQR = 9833 alias STUVE = "kuiue" I should be able to print as follows ABC JKL alias PQR alias STUVE Thanks Suresh (7 Replies)
Discussion started by: ssuresh1999
7 Replies

10. UNIX for Dummies Questions & Answers

Reading specific part of file

I have a requirement to go to particular line in the file and from there read the contents till it meets a particular criteria. For eg if the contents of the file is like 81 abcd ------------------- Line 1 82 cdfe ------------------- Line 2 83 dfj ------------------- Line 3 84 df... (5 Replies)
Discussion started by: guptan
5 Replies
Login or Register to Ask a Question

Featured Tech Videos