Need to capture certain text from a string in a different file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to capture certain text from a string in a different file
# 1  
Old 12-15-2010
Data Need to capture certain text from a string in a different file

Hi,

I wanted to know how i could accomplish this in a script using ksh.

Lets say there is a file called test.dat and it has a certain input like below :
Code:
.
.
 
Hi = 56
Hi = 67
.
.
 
1 record(s) selected

Now i need to capture the numbers after the = sign and store them in a variable.

I wrote a piece of code, but it doesnt seem to work and i dont know what i am doing wrong with this.

Here is the code :

Code:
count=0
{ while read myline;do
# process $myline
count=$(($count + 1))
word_count=`printf $myline | wc -c`
# select only rows having any data
# ignore header rows
if [ $word_count -gt 0 ] && [ $count -gt 3 ]
then
# Ignore the comments from sql statement
if [ $myline = *Hi* ] && [[ ! $myline = *record* ]]
then
TEXT=`expr substr $myline 5 2`
echo $TEXT
fi
fi
done } < test.dat

Thanks in advance

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 12-15-2010 at 10:03 AM.. Reason: code tags, please!
# 2  
Old 12-15-2010
Code:
$> VAR=`grep "^Hi = " infile| cut -d" " -f3`
$> echo $VAR
56 67

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 12-15-2010
It looks too busy as well as things like "if [ $myline = *Hi* ] && [[ ! $myline = *record* ]]" do not seem to be stable or fire for your line. Maybe use case, which I like for its form and extensibility, although there are many other ksh93 ways to test strings. {} not needed. No examples of the other lines you need to worry about for some reason. All ksh, no fork/exec costs.
Code:
TEXT=
while read l
do
 case "$l" in
 ("Hi = "[0-9][0-9])
   TEXT="$TEXT ${l#*= }"
   ;;
 esac
done < test.dat
echo "${TEXT# }"

This User Gave Thanks to DGPickett For This Post:
# 4  
Old 12-15-2010
Thank you guys for the amazing replies. They work.

I am going to go ahead and use zaxxon's way, as it kind of fits in what i actually want to do (this was just a snippet of what i actually intend to do), so special thanks to him Smilie

Thank You DGPickett as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Script to capture string in a log file

Dear all, I have a log file to be analysed. this log file contains vaiours lines of code starting with date timestamp. if my search string is exception then that resepective log statement starting from the date is required. example: 2014/10/01 16:14:44.459|>=|E|X|19202496|2832|... (5 Replies)
Discussion started by: shravee
5 Replies

4. Shell Programming and Scripting

String capture from ip file

Dear All From below mention input file I want op file as mention. Kindly help. IP file: "BSCGNR4_IPA17_C" 329 140119 0717 RXOCF-105 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Needed OP: 140119 0717 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Note that string mark in red as variable in... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

5. Shell Programming and Scripting

How to capture a string enclose by a pattern within a file?

Hi all, My file :test.txt just like this: ........................... From: 333:123<sip:88888888888@bbbb.com To: <sip:123456@aaaaa.com ......................... I want a script to capture the string between sip: & @ Expect output: 88888888888 123456 Please help! (4 Replies)
Discussion started by: Alex Li
4 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

7. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

8. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

9. Shell Programming and Scripting

capture output of file and send last string thereof to new file

Hello, If I run a program from within shell, the output is displayed in the command line terminal. Is there a way I can capture that output and choose only the very last string in it to send it to a new file? Thank you (6 Replies)
Discussion started by: Lorna
6 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question