Read and Replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read and Replace
# 1  
Old 06-29-2011
Read and Replace

Hi!
I think my last post was a little confusing, so I will try an post a sample this time.
I have a script which is supposed to output two columns of a file to a different file, then take the first column, search a directory for it, and replace anything it finds with the second column. This is what I have:

Code:
for file in /XXXX/XXXX/XXXX/I
do
     echo $VAR|awk '{printf "%d\n",$1}'|read NEWVAR
done

awk < XXXX_table.txt '{print $1 $3}' >> var1

for line in /XXXX/XXXX/XXXX/var1
do
    x=$(awk < var1 '{print $1}') 
    y=$(awk < var1 '{print $2}')
    cd /XXXX/XXXX/XXXX/I
    sed "s/$x/$y/g"
    echo $x
    echo $y
done

The first loop is supposed to remove all of the leading zeroes in all the files in that directory as well, but I guess that is wrong too.

Having lots of difficulty on this one, but I feel as though the answer is very simple

Last edited by fpmurphy; 06-30-2011 at 10:01 AM.. Reason: code tags please!
# 2  
Old 06-30-2011
Perhaps you would be good enough to explain to us what difficulty you are having?
# 3  
Old 06-30-2011
Actually now it is just the command to remove leading zeroes from all filenames in a directory.

They look like this:
Code:
XXX03XXX.00XXXXXXX.XXXXXXXX

And I want to remove just the leading zeroes on the second string if they exist. I have been trying to use this with no success.
Code:
echo $VAR|awk '{printf "%d\n",$1}'|read NEWVAR


Last edited by Franklin52; 06-30-2011 at 12:09 PM.. Reason: Please use code tags, thank you
# 4  
Old 06-30-2011
Hi

Fire this below command in the directory in which you want to rename:

Code:
$ for i in *
do
mv $i `echo $i | sed 's/\.0*/./'`
done

Guru.
# 5  
Old 06-30-2011
Excellent! that is perfect.
And one last thing! I am trying to replace one variable (string) with antoher variable using:
Code:
sed "s/$x/$y/g"

however, it seems as though this is incorrect, as it makes a new directory.

Last edited by Franklin52; 06-30-2011 at 12:10 PM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read contains within { and replace the word

Hello Team, I am looking for help to read contains from { } and replace the word. basically, I need to use this for nagios configuration file where each host/service define within {}. I want script or syntax to which read first word/line from {} and replace the desire word within that {}. ... (5 Replies)
Discussion started by: ghpradeep
5 Replies

2. Shell Programming and Scripting

Read from file and replace in format

Hi Guys, I am having below content in a file which is tab dimited file.txt 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 try("AUS ").ss("AUS_UP") try("NZ ").ss("NZ_UP") try("ENG ").ss("ENG_AP") (3 Replies)
Discussion started by: rohit_shinez
3 Replies

3. 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

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 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

6. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

7. Shell Programming and Scripting

Read Field from file1 and find and replace in file2

Hi All, I have file1 line below: $myName$|xxx Now I need to read the file1 and find for $myName$ in file2 and replace with xxx file1: $myName$|xxx file2: My name is $myName$ expected output in file2 after executing the script is below: my name is xxx Thanks, (8 Replies)
Discussion started by: gdevadas
8 Replies

8. UNIX for Dummies Questions & Answers

How read and replace Microsoft word in Unix

Hi, I'm newbie in UNIX. I would like to ask how to read a file which in Microsoft word format? Inside the doc file is just a few sentences . "It is a text. Help me with it". and I would like to replace the the word of text to message. I was trying with this for i in `find . -type f -name... (8 Replies)
Discussion started by: tee
8 Replies

9. 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

10. UNIX for Dummies Questions & Answers

How do I read/find/replace fields in a csv datafile?

hello. I'm somewhat a novice here so please be patient. My stumbling block when loading csvs into ORACLE tables is this: I need to read a csv datafile, check several fields in each line, and if any of stated fields contain A ZERO only then replace it with a null/blank character. I had a... (9 Replies)
Discussion started by: MrCarter
9 Replies
Login or Register to Ask a Question