473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Extract values from a file

2
Hello,
Can somebody help me on the below.
i am trying to write a bash script in my linux red hat system in order to do the follow
i have the below text file, lets say "file1.txt" and i want my script (script1.sh) to extract two specific values, and these two values to be used in another script (script2.sh)

file1.txt
text 1 in to use..............................15243
text 2 in to use..............................A
text 3 in to use..............................OR
text 4 in to use..............................UNLL
text 5 in to use..............................1
text 6 in to use..............................N
text 7 in to use..............................00BN/12345D
text 8 in to use..............................ABC
text 9 in to use..............................11ADF/123E
text 10 in to use.............................N
text 11 in to use.............................N
text 15 in to use.............................07/12
text 16 in to use.............................17-96
text 17 in to use.............................Y

how to use script1.sh to read file1.txt and get in the 7th line the value "12345" with out "D", and in the 9th line the "123" value with out "E".
these two values i need to use in script2.sh

thank you,
daku
Jul 22 '14 #1
6 4095
numberwhun
3,509 Expert Mod 2GB
I hate to say, but this sounds a lot like homework. That said, I will guide you, but not write this for you.

You will want to start with a bash tutorial. You can google and there are many out there. http://goo.gl/RyL9mS

If you want to post code, we can help guide you through it. Just post the code (in code quotes) and provide us the error(s) you are seeing and what is expected.

Regards,

Jeff
Jul 22 '14 #2
Luuk
1,047 Expert 1GB
another hint:
awk (or gawk) seems a tool which could be usefull

Because:
1) i know how to solve this with gawk
2) read the man-pages for gawk and decide for yourself ;)
Jul 22 '14 #3
daku21
2
hello,
i created a one line bash script but i am not good in programming/linux.

#!/bin/bash
awk '/text 7 in to use'/ file1.txt
#

-rw-r--r-- 1 root root 3584 Jul 23 10:55 file1.txt
-rwxr-xr-x 1 root root 10 Jul 23 11:16 script.sh

can you help or advice with 'awk' command to print out the needed values and assign to variable.

thnx,
Jul 23 '14 #4
sicarie
4,677 Expert Mod 4TB
You can look at what awk returns and how a shell script handles that. You can check out the awk manual page or just take a look at what is being printed out, and how to print the specific fields inside awk.

IMHO, I think you need a loop in a shell script to get down to the line you want (probably a for loop, but you could you a while if you wanted to), and then if you're on the right line, use awk to print out the values you want. I don't think you need variables, but a shell scripting tutorial would be able to tell you how to do that if you wanted to.
Jul 23 '14 #5
Luuk
1,047 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2. awk '/text 7 in to use/{ print substr($0,25,5)}/' file1.txt
  3.  
you should change the 25, and probable the 5 too

read here more about string functions in awk
Jul 23 '14 #6
Luuk
1,047 Expert 1GB
But, more advanced, this is also possible

Expand|Select|Wrap|Line Numbers
  1. awk -F '.' '/text 7 in to use/{ print substr($NF,6,5) }' file1.txt
  2.  
or even:
Expand|Select|Wrap|Line Numbers
  1. awk -F '/' '/text 7 in to use/{ print substr($NF,1,5) }' file1.txt
  2.  
Jul 23 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mohammad-Reza | last post by:
Hi I want to extract icon of an exe file and want to know how. I look at the MSDN and find out that I can use ExtractIconEx() Windows API but in there are some changes to that api in c# I made...
5
by: Bill | last post by:
Is there any way to programatically extract a cab file using C#? -- Bill Larson
3
by: steganos | last post by:
hi to all.... I have a problem. I know few the c but I need a program to extract from a file .doc all the characters A (for example) and to memorize them in a file counting them....
7
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and...
2
by: vishwa Ram | last post by:
Hi Dears, I want vb code, for extract xml file from Indesign. Thanks, vishwa Ram.
4
by: WT | last post by:
Hello, I am using themes and have seral themes for my site, user can choose it from a list then it is applyed to all pages by program. Problem is that some compoinent we use does not support...
3
by: learningvbnet | last post by:
Hi, I am trying to extract zipped files using Winzip in my VB.net application and I ran into 2 stone walls. 1. How do you handle file names with spaces. See psiProcess.Arguments For...
1
by: agarwalsunitadhn | last post by:
Hi I am developing an application in which i need to convert a text file into a resource file and then serach different resources from the resource file. I want to know how to create the resource...
2
by: sandeepshetty | last post by:
hi... Im new to c#.Net and right now working on Roof truss designing project using c#.net(visual studio 2005 ) .For storing the dimensions(values) of these trusses I am using ms-sql server...
5
by: moroccanplaya | last post by:
i got a program that creates a file then adds a file on top of that file so how would i go on and extract this file? here is my code.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.