print pattern within double quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print pattern within double quotes
# 1  
Old 06-16-2009
print pattern within double quotes

Hi All,
I have multiple lines in a file like:-
"abc" def "ghi" jkl "mno" 1

I want to print in output:-
abc/ghi/mno 1

How can I do this in perl?

Regrds,
Nilabh

-----Post Update-----

Additional info:-
The last field of the file should be output as it is.In the above example 1 is the last field in the file

-----Post Update-----

Additional info:-
The last field of the file should be output as it is.In the above example 1 is the last field in the file
# 2  
Old 06-16-2009
que sound track......

What have you tried so far to solve your programming requirements?

Posting your question doesn't count Smilie
# 3  
Old 06-16-2009
Hope this helps point you in the right direction:

#!/usr/bin/env perl

use strict;

my $file="file.txt";
open(FILE, "<$file") or die "Unable to access $file: $!\n";
while(<FILE>) {
if($_ =~ /\"(.*?)\"\s.*?\s\"(.*?)\"\s.*?\s\"(.*?)\"\s(.*)/) { print "$1/$2/$3 $4\n"; }
}
close(FILE);
exit(0);
# 4  
Old 06-16-2009
sigh.... Like I said Neo.... (sad thing is) they prove you wrong all the time on this forum. Smilie

-----Post Update-----

Quote:
Originally Posted by in2nix4life
Hope this helps point you in the right direction:

#!/usr/bin/env perl

use strict;

my $file="file.txt";
open(FILE, "<$file") or die "Unable to access $file: $!\n";
while(<FILE>) {
if($_ =~ /\"(.*?)\"\s.*?\s\"(.*?)\"\s.*?\s\"(.*?)\"\s(.*)/) { print "$1/$2/$3 $4\n"; }
}
close(FILE);
exit(0);
A contrived solution that only works for the exact sample data posted. But maybe thats all they need. Smilie

-----Post Update-----

christ, this post update thing is a bit obnoxious.....

-----Post Update-----

christ, this post update thing is a bit obnoxious.....

-----Post Update-----

aHHHH...hit refresh and it posts again!! Now I understand why a lot of people seem to be repeating themselves

-----Post Update-----

aHHHH...hit refresh and it posts again!! Now I understand why a lot of people seem to be repeating themselves

-----Post Update-----

doh!!

-----Post Update-----

doh!!
# 5  
Old 06-16-2009
Thank you for the solution

I was trying
$_ =~ /".*"/;
but it wasnt wrking

The only issue is that I dont have only 4 fields.I have different line length.Hence Please suggest me a program where it greps for all strings between double quotes and also prints the last field of the line
# 6  
Old 06-16-2009
Code:
$foo = '"abc" def "ghi" jkl "mno" 1';
$foo =~ s/\s+\S+\s+/\//g;
$foo =~ tr/"//d;
print $foo;

Now what other exceptions to your sample data are there? Unbalanced quotes? Embedded quotes? No white space before the last column?
# 7  
Old 06-17-2009
Code:
while(<DATA>){
	my @tmp=$_=~/(".*?"|[^ ]+$)/g;
	print join "/", map {s/"//g;$_} @tmp[0..$#tmp-1];
	print " ",$tmp[$#tmp];
}
__DATA__
"abc" def "ghi" jkl "mno" 1
"ABC" def "GHI" 2
"111" def "222" jkl "333" hlm "444" 3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add double quotes to the words after given pattern

Hi, I have a text file with different results and I would like to add single quotes to the value after the given pattern '=' This would be the original text file: user_id=7492 and key=clickid; user_id=7867 and key=clickid; user_id=8649 and key=clickid; And I would like the output to be... (9 Replies)
Discussion started by: mac-arrow
9 Replies

2. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

3. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

4. Shell Programming and Scripting

awk print - fields separated with comma's need to ignore inbetween double quotes

I am trying to re-format a .csv file using awk. I have 6 fields in the .csv file. Some of the fields are enclosed in double quotes and contain comma's inside the quotes. awk is breaking this into multiple fields. Sample lines from the .csv file: Device Name,Personnel,Date,Solution... (1 Reply)
Discussion started by: jxrst
1 Replies

5. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

6. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

7. Shell Programming and Scripting

Using echo to print double quotes along with variable substitution

Hi, I am generating html code using cshell, but i am having one problem while printing double quotes, I need to write following code in file. where $var contains list of web address <a href="$var">$var</a> So i am using echo "<a href="$var">$var</a>" > file.html But with this " in... (4 Replies)
Discussion started by: sarbjit
4 Replies

8. Shell Programming and Scripting

Single quotes and double quotes

Hi guys, I have a sed line in double quotes which works fine, but I want it to be in single quotes here is the sed line sed "/abc_def/s/\'.*\'/\'\${abc_def}\'/" can some one give the equivalent to the above script in single quotes Thanks a ton (5 Replies)
Discussion started by: sol_nov
5 Replies

9. Shell Programming and Scripting

Dont want to print double-quotes

Hi, I have a perl script which generates an LDAP report in CSV Format. Issue: Some of attribute values has double-quotes as fed by users. I dont want to print those double-quotes while printing the attribute values. Please let us know how I can overcome this issue. Thank You. ... (2 Replies)
Discussion started by: gazalinawaz
2 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies
Login or Register to Ask a Question