capturing the value in file before string(*) using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting capturing the value in file before string(*) using sed
# 1  
Old 01-18-2012
capturing the value in file before string(*) using sed

I am trying to capture the last value before the the * in test4.txt file using sed & tail (in this case 0FA). I got the output using the below steps.

P.s: The number of * in the file varies from file to file.

Any idea of a better way to do this?

Code:
cat test4.txt

                           1234  Not Visible                     0   00  046
                           5678  Not Visible                     0   00  047 *
                           0123  Not Visible                     0   00  0F0
                           0234  Not Visible                     0   00  0F1
                           0567  Not Visible                     0   00  0F2
                           0789  Not Visible                     0   00  0F3
                           1256  Not Visible                     0   00  0F4 *
                           0912  Not Visible                     0   00  0F5
                           0546  Not Visible                     0   00  0F6
                           0678  Not Visible                     0   00  0F7
                           0734  Not Visible                     0   00  0F8
                           1432  Not Visible             (M)    0   00  0F9
                           -     AVAILABLE                       0   00  0FA *
    Total                  ----
    Mapped Devices:          42
    Including Metamembers:  311
    Available Addresses:   2612 (s)


Legend for Available address:
Code:
(*): The VBUS, TID, LUN address values represent a gap in the
     address assignments or are the next available address in
     the run
(s): The Available Addresses for a director are shared among
     its ports (shared)

Code:
sed -n '/*/p' test4.txt  >>test3.txt

tail -2 test3.txt |sed 's/.*\(....\)*.*/\1/' >> /var/tmp/test2.txt

sed '$d' lun2.txt >> /var/tmp/test1.txt

rm /var/tmp/test4.txt
rm /var/tmp/test3.txt
rm /var/tmp/test2.txt

cat test1.txt 
OFA


Last edited by Franklin52; 01-18-2012 at 05:31 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-18-2012
Try this...
Code:
sed -n 's/.* \(.*\) \*$/\1/p' infile
047
0F4
0FA

#or

sed -n 's/.* \([0-9A-Z]*\) \* *$/\1/p' infile
047
0F4
0FA

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 01-18-2012
In nawk ..
Code:
$ nawk '/\*/{print $(NF-1)}' infile
047
0F4
0FA

This User Gave Thanks to jayan_jay For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. UNIX for Dummies Questions & Answers

SED capturing information from a variable

Hi I can't get this line to work. I need to capture what is between Home and plugin_out, where the .* is located. RUN=`echo '${TSP_FILEPATH_PLUGIN_DIR}' | sed -e 's^/results/analysis/output/Home/\(.*\)/plugin_out/g'`; echo ${RUN}; I'm getting the error sed: -e expression #1, char 51:... (1 Reply)
Discussion started by: jdilts
1 Replies

4. Shell Programming and Scripting

capturing the value in file before string(*) and the similar value in next line only

I've the output in the file like below. I want to capture the value in file before string(*) and the similar value in next line only. cat test1.txt 0003 Not Visible (M) 0 00 03F 0005 Not Visible (M) 0 00 040 - AVAILABLE 0 00... (1 Reply)
Discussion started by: sai_1712
1 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

7. Shell Programming and Scripting

Capturing the string and the string below it

Hi, I want to read the following input and want to produce an output as given below. Input: CAR DESIGN COLOUR SERVICE MERZ APPLE RED 2 YEARS ORANGE GRAPE ... (7 Replies)
Discussion started by: bha148
7 Replies

8. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

Capturing string between a xml tag

Hi All, I have an XML-: <ProcId>CES_P5010_AddVLan</ProcId> <DataVersion>yxcxycyxcycyxc</DataVersion> <JobId>OR3000055-002-1</JobId> </CesHeader> <VLanServiceList> <NopId>blu</NopId> </VLanServiceList> <StatusNPA>2</StatusNPA> ... (5 Replies)
Discussion started by: amit_iti
5 Replies
Login or Register to Ask a Question