Problems with sed and flat file variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problems with sed and flat file variables
# 1  
Old 08-06-2007
Bug Problems with sed and flat file variables

Hello All,

It has been a loooooooooooong time since I had last used sed but decided to use it for a simple task I have .

My goal is to use sed to read variables from a flat file then use those same variables in order to make some subsitutions. However what I am finding is that when the variable is in fact substituted there is an additional block looking characted at the enf of the variable, thus throwing off my script.

My goal is to remove this [] character from the substituted variable however I am having a devil of a time doing so.

Does anyone have any suggestions or experience with such a task?


Here is my code as it stands:


#!/usr/bin/ksh
#test out mapping and clearing WS Cache for night migrations



cat /cygdrive/c/temp/misc/array.txt | while read ARRAY
do



sed -e "s/csslnxyz/$ARRAY/" -e 's/K/\n/' -e 's/xyz1_84617/FCH1_84617/' -e 's/xyzPRD/FCHIPRD/' ws_template.txt >> first_output1.txt

done



The format if the flat file (array.txt) is as follows:

cssln123(return)
cssln124(return)
cssln125(return)


The issue I can see is that when I do testing and echo the $ARRAY values there is indeed extra return character shown.

For example I would expect (echo $ARRAY;echo "is my variable") to look like:

cssln123 is my variable

However in testing I see:
cssln123
is my variable


And this is my issue. I need to find a way to remove those extra EOL chars either before performing or after performing the substitution , whicever is easier.

YOUR HELP IS MUCH MUCH APPRECIATED!!!

Thanks!
# 2  
Old 08-06-2007
Code:
$ cat ./inputfile.txt
cssln123(return)
cssln124(return)
cssln125(return)
$ cat ./array.sh
#!/bin/bash

counter=0
while read line; do
   array[${counter}]=$( echo "${line}" | sed 's/^\([^(]*\)(.*$/\1/' )
   (( counter = counter + 1 ))
done < ./inputfile.txt

counter=0
for element in ${array[@]}; do
   echo "\$array[$counter] is ${element}"
   (( counter = counter + 1 ))
done

exit 0
$ ./array.sh
$array[0] is cssln123
$array[1] is cssln124
$array[2] is cssln125

Cheers,
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed flat file manipulation

Hello, I have a large flat file where i need to change data in columns 131-133 based on what is in columns 172-173. I am not sure if I need to read the file line by line and make the change or if I can do this in a single statement. thank you (3 Replies)
Discussion started by: gblmin
3 Replies

2. Red Hat

Problems with EURO symbol in a data flat file

Hi, I'm creating a flat file with various deimiters in Linux RHEL. the content for the flat file will be extracted from an oracle database. delimiters also stored in a seperate table in oracle databasae. when i use extended ASCII characters like cedilla or EURO (€) symbols, the actual character is... (9 Replies)
Discussion started by: Fakru.y
9 Replies

3. Shell Programming and Scripting

Sed, replace variables in file

Hi Everyone, I need some help with sed and I'm totally new to it. I have a template file with variables in it. These variables start with a '$' sign and are exactly one character long (plus the '$' sign). For example: $a, $b, etc. Each variable should be replaced with the contents of a... (9 Replies)
Discussion started by: csarli2006
9 Replies

4. Shell Programming and Scripting

Creating Dynamic Variables from a Flat File

Greetings all, Been trying to do my Googling and forum searches but can't seem to lock in on a solution. I have a script that parses a log and collects all the uniq events to a flat file. Some days might have 50 unique events, other days might have 75. (Hence my reference to dynamic.) ... (2 Replies)
Discussion started by: sjrupp
2 Replies

5. Shell Programming and Scripting

environment variables in a sed script file

Hello Everyone I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this: { a\ STRINGTABLE DISCARDABLE\ BEGIN\ 5, 150 {a\ #define RC_SHELL, "%ID_SHELL%"\ #define RC_NAME, "%ID_NAME%"\ END } ... (1 Reply)
Discussion started by: edgarvm
1 Replies

6. Shell Programming and Scripting

SED | Awk flat file one liner

sed awk one liner (2 Replies)
Discussion started by: jap2614
2 Replies

7. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

8. Shell Programming and Scripting

How to delimit a flat file with records with SED

Hi gurus, hoping someone can help with a sed line that can do the following... I have a flat file with about 1000 records, but in order to import into openoffice spreadsheet, I need to create a delimited file. I'd like to do 2 things with the SED command: 1- add a pipe character "|" at the end... (4 Replies)
Discussion started by: RogCor
4 Replies

9. UNIX for Dummies Questions & Answers

sed file problems

when i am running a sed command i want to get rid of all of the backslashes in the lin but it is taking this as being a command how do i delete backslashes????? sed -e "s/\/g" Anyn ideas????????? (7 Replies)
Discussion started by: johnnynolegs
7 Replies

10. UNIX for Dummies Questions & Answers

variables use upper case? sed : output to the same file?

Hi, Q1: are the variables in shell script usually UPPER CASE? Q2: can sed output to the same file that it's using it? eg. cat sameFile | sed 's/here/there/g' > sameFile ? I expect the sed replace all "here" to "there" and change it in sameFile. i tried that one, the sameFile... (1 Reply)
Discussion started by: gusla
1 Replies
Login or Register to Ask a Question