Read from file and replace in format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from file and replace in format
# 1  
Old 02-07-2017
Read from file and replace in format

Hi Guys,

I am having below content in a file which is tab dimited

file.txt
Code:
AUS AUS_UP
NZ NZ_UP
ENG ENG_AP

I need to read the file content and replace it with below output format one to one replace
Code:
try("AUS ").ss("AUS_UP")
try("NZ ").ss("NZ_UP")
try("ENG ").ss("ENG_AP")

# 2  
Old 02-07-2017
With 41 other requests for help over the last 3 and a half years, we would hope that you have some idea of how to do things like this on your own.

What have you tried to solve this problem?

What operating system are you using?

What shell are you using?
# 3  
Old 02-07-2017
Hi,

I am using sun solaris OS and bash

code i have tried

Code:
while read -r i; do
   printf "try("$i ").ss("$i")<%s>\n" 
done < test.txt

try(AUStry(NZtry(ENG1

But not able to break the space dilimted
# 4  
Old 02-07-2017
Note that there are two fields in your input file; not just one.

Note that the format string presented to printf needs to be a single operand and (since double-quotes do not nest), the format string you used is three operands (and the last two were ignored since there is no format specifier in the 1st operand that refers to them).

Try this instead:
Code:
while read -r f1 f2
do	printf 'try("%s ").ss("%s")\n' "$f1" "$f2"
done < file.txt

If you want to include double-quotes in the output and you're using double-quotes to delimit the format string you need to escape the inner quotes:
Code:
while read -r f1 f2
do	printf "try(\"%s \").ss(\"%s\")\n" "$f1" "$f2"
done < file.txt

but I find using single-quotes to delimit the format string easier to write and easier to read.
This User Gave Thanks to Don Cragun 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

Read a file and replace values in a script

Hi , I have a property file placed in folder /usr/opt/temp/aorc.prop which has values given below . I need to read this file content and replace the node with actual values in a shell script . Each time the script shall replace the node value from the poperty file and execute a cfsend command and... (10 Replies)
Discussion started by: samrat dutta
10 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

File read format issue in UNIX

hi all. my loop is getting failed eventhoug it is 1=1 but it is failure message. any help plz Output expected : echo "sucesss" code out=`cat bit.txt` if ]; then echo "sucess" else echo "Failure" (2 Replies)
Discussion started by: arun888
2 Replies

4. Shell Programming and Scripting

Read file and replace a particular line if found

Hi All There is another challenge which stand in front of me. And want all to have the experience with that I have a file in Unix say a.txt. What I was trying is to read the file line by line and matching the line to particular pattern, and if that pattern found I want to replace that line... (5 Replies)
Discussion started by: adisky123
5 Replies

5. Shell Programming and Scripting

Read from text file;format and print output

Hi Following is the assumed input... Symmetrix ID : 12345 Originator Port wwn : 123456789 User-generated Name : 123456789/123456789 Sym Dev Dir:P LUN ------ ----- ----------------------- ---- --- ---- ---- ---- ------- 1234 ... (4 Replies)
Discussion started by: maddy.san
4 Replies

6. Shell Programming and Scripting

Read from one file-Replace a pattern in another with the current one

Hi Friends, I have a text file like this cat main.txt I like this website cat website > new_website grep website > hello_website cat replace.txt hello unix apple Now, for each line read in 2.txt, I want the pattern "website" in 1.txt to be replaced with it. Basically, I... (9 Replies)
Discussion started by: jacobs.smith
9 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

Read a file and put it in HTML format

Hi, I have one file as follows and I need to read this file contents in an HTML format. And send html file to some mail ids using sendmail. Communications Pvt Ltd Report AccountId Name Code IdBill Balance ... (3 Replies)
Discussion started by: Kattoor
3 Replies

9. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies

10. Shell Programming and Scripting

Replace characters then read the file without changing it

Hi All At the moment the following code works but ideally i do not want to have to change the original $1 tr "\r" "\n" < "$1" > "$1.fix" printf "\n" >> "$1.fix" mv "$1.fix" "$1" FILE=$1 coffee_out="splitmovie" coffee_fill="-splitAt" coffee_end="-self-contained -o output.mov $2"... (1 Reply)
Discussion started by: babajuma
1 Replies
Login or Register to Ask a Question