Help on sed requested


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on sed requested
# 1  
Old 10-27-2010
Help on sed requested

Hi

I have a problem to resolve, I think sed is the best option, and I am not successful yet.

Have a UNIX file which has records as of the 2 character state codes like
NY
NJ
PA
DE

From the file I need to create this as a variable in the same script or another file - ('NY','NJ','PA',DE')

The file can contain more state codes.

The output need to be passed on to a sequel in the where clause -
select * from taxcode
where state_code in ('NY','NJ','PA',DE')

So far I am successful only in adding a comma to all the 4 lines of records.

Please help.
# 2  
Old 10-27-2010
Code:
sed "s/.*/'&'/" file | tr '\n' ',' | sed "s/,$//"

# 3  
Old 10-27-2010
Thanks anbu23 for the quick help.
i tried, but it did not work, i tried to write the output to a file like this

sed "s/.*/'&'/" file | tr '\n' ',' | sed "s/,$//" > myfile

myfile was created with 0 byte.

how will you get the output to a variable?

thanks again for the help.
# 4  
Old 10-27-2010
hi,
Anbu code works as expected and redirection also works fine.

To get output in variable:

Code:
a=`sed "s/.*/'&'/" 1_ip | tr '\n' ',' | sed "s/,$//"`
echo $a


Last edited by dragon.1431; 10-27-2010 at 05:22 PM.. Reason: added text info
# 5  
Old 10-27-2010
Code:
$ cat cod
NY
NJ
PA
DE

$ var=$(echo $(<cod) | sed "s:^ *:(\':;s: *$:\'):;s: :\',\':g")
$ echo $var
('NY','NJ','PA','DE')
$ echo "select * from taxcode where state_code in $var"
select * from taxcode where state_code in ('NY','NJ','PA','DE')
$

# 6  
Old 10-27-2010
try to apply Anbu's code step by step so that where you get error maybe you missing something in syntax?

Code:
sed "s/.*/'&'/" file | tr '\n' ',' | sed "s/,$//"

1. Did first part of command added " ' " around each fields like 'PA'?
2. Did second part changed newlines to commas?
3. the commas at the end of each line were removed?
# 7  
Old 10-27-2010
Thanks everyone, Thanks ctsgnb.
It worked.

Making no empty promises, but I will try my best to come back with - the function of each symbols in the command.


---------- Post updated at 05:20 PM ---------- Previous update was at 05:19 PM ----------



sorry, missed to add this -
the function of each symbols in the command - which ctsgnb used
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

The requested URL was rejected. Please consult with your administrator

Ggod evening. I need your help please, in a Production system there is a process that download a xls file from an URL which is IMF(International Monetary Fund) and afterwards to be loaded into a databse table. When testing conectivity from a unix server to IMF seems to work but when editing it... (9 Replies)
Discussion started by: alexcol
9 Replies

2. Shell Programming and Scripting

SQL Script HELP Requested.

Hello ALL , i am requesting help on for this script i am preparing to get the result of a query in a excel sheet : current Error: Script : NO Excel file created. requesting to know where i am going wrong. #!/bin/ksh... (2 Replies)
Discussion started by: anirudhkashikar
2 Replies

3. Forum Support Area for Unregistered Users & Account Problems

Sign in issues -- additional info as requested

User name: Michael Mullig <removed email addresses> (1 Reply)
Discussion started by: Mike Mullig
1 Replies

4. Shell Programming and Scripting

Newbie bash scripting help requested

Hi, I'm very new to bash scripting and Linux in general. I'm running Ubuntu Server 10.04 and trying to write a bash script to launch a program. In doing so, I've come across a couple of things that I obviously don't understand. Here is a short script that exemplifies those things: ... (9 Replies)
Discussion started by: Carson Dyle
9 Replies

5. Shell Programming and Scripting

Help requested for a script with sed

Hello Folks, I would very much appreciate if I could get help/suggestions on a particular sed usage. I have to write a script to take version info from a version file, compute the image name, print error if the image does not exist. The version file looks like below: " # # version.cfg #... (3 Replies)
Discussion started by: fatimap
3 Replies

6. UNIX for Advanced & Expert Users

assistance requested (sed related)

I gotta write a command to change the accounts in /etc/passwd that use a shell other than the bash to bash shell. those accounts that dont use a shell shouldnt get modified. assuming all the shell programs end in sh and other programs dont. and the result should go into /etc/passwd.rev any hint? (4 Replies)
Discussion started by: metalwarrior
4 Replies

7. Shell Programming and Scripting

AWK issue--> Help requested

Fairly new scripter so please bare with me if what I have done below is not according to standards. Okay...heres what I am trying to do. I have a pattern that I need to search for in a directory. This gives me a list of files that includes a control file that contains totals of the line nos for... (3 Replies)
Discussion started by: alfredo123
3 Replies

8. AIX

AIX 4.1.5/RS6000 boot hang, help requested

Hello there! I have a RS-6000 7043-140 machine with AIX version 4.1.5, that is working for almost 8 years now. It has a tty monitor. My problem started when I upgraded my machine to install a gxt250 graphics adapter card together with a 15" AOC VGA Monitor, logitech keyboard and mouse, ... (2 Replies)
Discussion started by: bright_genius
2 Replies

9. Solaris

Your Opinion requested

Ladies/Gentlemen, I am looking for a web-based tool to keep track of my Sun inventory. The following list of fields are fields I would like to store: Root Passwd (needs to be secure) / Hostid / Console Port / IP Address / Platform / Application / Hostname . . . you get the point. Do any of... (4 Replies)
Discussion started by: pc9456
4 Replies

10. Email Antispam Techniques and Email Filtering

help requested: procmail receipes

Hello all. I want some help procmail receipe. I tried to get some mails' sender and receiptient. Then tried to send them a bash script. But it didnot work. I try a lot of variation of the below receipe. Could anyone can help what is wrong on my receipe? Or if the recepie is correct what can be... (0 Replies)
Discussion started by: kedi
0 Replies
Login or Register to Ask a Question