sed - extracting the first word only if match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - extracting the first word only if match
# 8  
Old 10-03-2013
Code:
awk -F"[ =]" '		#setting the field separator to space or equal
	/WORD1/ {	#search for lines containing "WORD1"
	print $3	#print filed number 3
	}' file

This User Gave Thanks to Jotne For This Post:
# 9  
Old 10-17-2013
Thank you every body for your help.
The simple test I made with sed in shell script work well from your recomendations.

Now I have written a small piece of code using Qt Creator.

My question is about regular expression not Qt.

In term of regular expression, what is the meaning of :
Code:
"code=(.+)\\s"

I think that my problem arise because I must tell (don't know how !!) that the end of the word is the first encountered blank, not the last one if there are more than one.

As you know my purpose is to extract some character code. For that I am using this code :
Code:
QString town_code;
QRegularExpression re1("code=(.+)\\s");
QRegularExpressionMatch match = re1.match (a_text);
hasMatch = match.hasMatch();
if (hasMatch) {
town_code = match.captured(1);
............
............
............
}

This code work in some conditions, and failed in other:

The result is correct for:
Code:
ARANTZAZU (OŅATI) code=onati-id20059 ville=OŅATI

The town_code variable contains the correct value:
Code:
onati-id20059

The result is incorrect for:
Code:
OTSAURTE (ZEGAMA) code=Zegama-id20025 ville=ZEGAMA tameteo=http://www.tameteo.com/meteo_Zegama-Europe-Espagne-Guipuscoa--1-3385.html

The town_code variable contains the incorrect value:
Code:
Zegama-id20025 ville=ZEGAMA

Any help is welcome.
# 10  
Old 10-17-2013
I haven't used Qt, but I believe it's:
code= - the literal code=, followed by
(.+) - one or more of any character as bracket expression #1, followed by
\\s - whitespace (normally just \s, but I assume it's escaped for C++ strings)

The bracket expression is so you can extract that matched part afterwards (presumably by match.catured(1)).

EDIT: Your regex is matching too much - a regular expression will consume as much of the string as it can. In this case, that's anything from code= up to (but excluding) the last word on the line.

Rather than using (.+) to match any character, try using ([^\\s]+) to match non-whitespace characters (as in the earlier sed suggestions).

Last edited by CarloM; 10-17-2013 at 08:30 AM..
This User Gave Thanks to CarloM For This Post:
# 11  
Old 10-17-2013
Quote:
Originally Posted by CarloM
Rather than using (.+) to match any character, try using ([^\\s]+) to match non-whitespace characters (as in the earlier sed suggestions).
Great.

Seems working on some tries
.
Thank you very much.

But could you explain the syntax.
# 12  
Old 10-17-2013
[^\\s] defines an inverse character set - i.e. match any character except those specified (in this case, match anything except whitespace).
This User Gave Thanks to CarloM For This Post:
# 13  
Old 11-06-2013
Quote:
Originally Posted by CarloM
[^\\s] defines an inverse character set - i.e. match any character except those specified (in this case, match anything except whitespace).
Thank you for taking time for helping.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed script to delete the last word after a last pattern match

Hi Guys , I am having a file as stated below File 1 sa0 -- i_core/i_core_apb/i_afe0_controller/U261/A sa0 -- i_core/i_core_apb/i_afe0_controller/U265/Z sa1 -- i_core/i_core_apb/i_afe0_controller/U265/A sa1 -- i_core/i_core_apb/i_afe0_controller/U268/Z sa1 -- ... (7 Replies)
Discussion started by: kshitij
7 Replies

2. Shell Programming and Scripting

Replacing the first word if the word three match

Dear ALL, I have sample file : IDcentos-forum,bash,linuxCentOS,GNome IEfedora-milis,cli,linuxRedhat,KDE IRfreebsd-milis,aix,unixbsd,pyton required output: centos,bash,linuxCentOS,GNome fedora,cli,linuxRedhat,KDE freebsd,aix,unixbsd,pyton Can you help me pls.. (1 Reply)
Discussion started by: gnulyn
1 Replies

3. Shell Programming and Scripting

Replacing first word while extracting

Hello All, I am extracting a part of file. the file looks as follows USING CHARACTER SET UTF8 DEFINE JOB ( DEFINE SCHEMA Flat_File_Schema ( cntnt_id VARCHAR(10) ); DEFINE OPERATOR o_mload TYPE update SCHEMA * ATTRIBUTES ( VARCHAR TdpId = @TdpId (5 Replies)
Discussion started by: nnani
5 Replies

4. UNIX for Dummies Questions & Answers

Extracting part of a word

I have the code message={TP=2012:09:23:00:00:00:GMT,SD=2012:09:23:00:00:00:GMT,SP=2,FT=CCGT,FG=3605} I want to extract the FG=3605 parts of this. Please help. I am trying to do this using awk or unix. (5 Replies)
Discussion started by: JenniferTopham
5 Replies

5. Shell Programming and Scripting

Problems extracting word using SED

I have a file containing strings such as: UPDATE PS_CA_BI_FF2_TA3 SET DELETE_ME = 'Y' WHERE PROCESS_INSTANCE BI.LAST_UPDATE_DTTM FROM PS_CA_BP_LINES LINE, PS_INTFC_BI BI WHERE EXISTS ( SELECT 'X' FROM PS_CA_BILL_PLAN BP WHERE BP.CONTRACT_NUM %Select(COUNTER4) SELECT COUNT(*) FROM PS_INTFC_BI... (2 Replies)
Discussion started by: simpletech369
2 Replies

6. Shell Programming and Scripting

Extracting a word from a variable

Hi Guys, Need you quick assistance on the below, trying to extract a word from a variable i.e. acmi101acmi102acmi103acmi104 When i use the following code awk '{gsub(/cmi102/,"")};1' it leaves a space in the variable, need to get rid of the space that it leaves. Any ideas. the above... (3 Replies)
Discussion started by: eo29
3 Replies

7. Shell Programming and Scripting

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot (6 Replies)
Discussion started by: jamestan
6 Replies

8. UNIX for Dummies Questions & Answers

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot very (0 Replies)
Discussion started by: jamestan
0 Replies

9. Programming

sed no match word end with hyphen

Hi, This is my first post.It has two parts first part: I want to match a line that starts with whitespace tab or similar followed by must start with specific word and not match same word start with hyphen like this: grep height file1: height: 150px; line-height: 1.5em; height:... (4 Replies)
Discussion started by: medium_linux
4 Replies

10. Shell Programming and Scripting

Extracting from second word

Hi all, I need to extract the Particular string from the whole word,the input file is : 123,345,aaaa,555,....,.... I need all the record from 345 so i need to eliminate the first record. Output: 345,aaa,5555,....,.....,..... Thanks in advance. (3 Replies)
Discussion started by: ithirak17
3 Replies
Login or Register to Ask a Question