how to add single digit in front of the word and line in the file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add single digit in front of the word and line in the file.
# 1  
Old 04-11-2011
MySQL how to add single digit in front of the word and line in the file.

Hi ,

how to add the single digit to front of the word and front of the lines in the one file with compare pattern file and get digit. like example

pattern file pattern.txt
Code:
pattern num
bala 2
raja 3
muthu 4

File Name: chennai.dat
Code:
muthu is good boy
raja is bad boy
selvam in super boy
then bala is very good boy

the above two files are there. check the chennai.dat file and compare the pattern from pattern.txt file and place the respective digit to chennai.dat file.

Last edited by Franklin52; 04-12-2011 at 03:23 AM.. Reason: Please use code tags
# 2  
Old 04-11-2011
is this what you wanted?

Code:
awk 'FNR==NR && FNR>1{a[$1]=$2} NR>FNR{$1=a[$1] FS $1; print $0}' pattern.txt chennai.dat

output:
Code:
4 muthu is good boy
3 raja is bad boy
 selvam in super boy
 then bala is very good boy

# 3  
Old 04-12-2011
Hi,

Thanks!.
but i want output similar but slightly changes....in the output file need this type..

4muthu is good boy
3raja is bad boy
selvam in super boy
then 2bala is very good boy

Thanks in advance.
# 4  
Old 04-12-2011
Code:
cat pattern.txt  | while read LINE
do
    NAME=`echo $LINE | cut -f1 -d " "`
    NUM=`echo $LINE | cut -f2 -d " "`
    
    cat chennai.dat | sed "s:$NAME:$NUM$NAME:g" > tmp_file.dat
    cat tmp_file.dat > chennai.dat
done

# 5  
Old 04-12-2011
Code:
#!/bin/ksh
while read mLine
do
  while read mName mValue
  do
    mLOut=$(echo ${mLine} | sed "s/${mName}/${mValue}${mName}/g")
  done < Pattern_File
  echo ${mLOut}
done < Main_File

# 6  
Old 04-12-2011
Code:
awk 'FNR==NR && FNR>1{a[$1]=$2} NR>FNR{ for(i=1;i<=NF;i++) if($i in a) $i=a[$i]$i; print $0}' pattern.txt chennai.dat

This User Gave Thanks to sk1418 For This Post:
# 7  
Old 04-14-2011
Thanks but i have few files.and i added one more tab in pattern file.
example: pattern.txt
code pattern num
ax bala 2
ax raja 3
ax muthu 1
ay bala 2
ay raja 3
ay muthu 3
az bala 2
az raja 5
az muthu 6

The report file have a code in the firest line. so first we check the file code compare with pattern file. then will append the digt in the each lines with compare pattern in pattern file to report file.

---------- Post updated at 03:52 PM ---------- Previous update was at 12:00 PM ----------

Quote:
Originally Posted by sk1418
Code:
awk 'FNR==NR && FNR>1{a[$1]=$2} NR>FNR{ for(i=1;i<=NF;i++) if($i in a) $i=a[$i]$i; print $0}' pattern.txt chennai.dat

this script is not working....

---------- Post updated 04-14-11 at 10:52 AM ---------- Previous update was 04-13-11 at 03:52 PM ----------

hi All,

i want to add the single digit front of the line in the report file and string compare with pattern file.

patter file: pattern1.txt

pattern num
like 4
love 3
john 2

report file: report.txt

i like very much
but bala is good boy
i will tell john

so after execute the script out is report.txt file

4 i like very much
2 but john is good boy
3 i will love u

---------- Post updated at 11:26 AM ---------- Previous update was at 10:52 AM ----------

Help to correct the below script. the script is checking with pattern and place the digit front of the word but i need to change front of the line.


for i in `ls *.R`
do
cat sch.txt | while read LINE
do
CLT=`echo $LINE | cut -f1 -d "|"`
NAME=`echo $LINE | cut -f2 -d "|"`
NUM=`echo $LINE | cut -f3 -d "|"`
echo " $NAME $NUM$NAME "
comp_string=`grep "%s" $i| cut -d ":" -f2 | cut -d "|" -f1`
if [ $CLT = $comp_string ]
then
cat $i | sed "s:$NAME:$NUM$NAME:g" > tmp_file.dat
cat tmp_file.dat > $i
else
echo " Not Matching CLient Code"
fi
done
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking a single digit in a file using Grep

Hi All, I have a file which keeps count based on completion of a certain activity. I am using the following grep command to return a '1' in case the count is zero grep -ic "0" abc_count.txt Now the issue happens when the count is '10', '20' etc .. in these cases as well it returns a... (5 Replies)
Discussion started by: dev.devil.1983
5 Replies

2. Shell Programming and Scripting

Insert a single quote in front of a line in vi editor

Hello Gurus, I wanted to put a single quote in every where starting with /oradata, and at the end with .dbf. For example I have one line as below: alter database rename datafile /oradata/test.dbf to /oradata_new/test.dbf I wanted as below alter database rename datafile '/oradata/test.dbf' to... (3 Replies)
Discussion started by: pokhraj_d
3 Replies

3. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

4. Shell Programming and Scripting

how to delete the line if the first letter is a single digit

Hi, I'm trying to acheive the following, I have a dat file in which i have several addresses, If the address starts with a single digit then i have to delete the line, if it starts with 2 or more digits then i have to keep the line Here is a sample of my file: 377 CARRER DE LA... (5 Replies)
Discussion started by: ramky79
5 Replies

5. Shell Programming and Scripting

word counts for a single line xml file

I have any XML ouput file(file name TABLE.xml), where the data is loaded in A SINGLE LINE, I need help in writting a ksh shell script which gives me the word counts of word <TABLE-ROW> This is my input file. <?xml version="1.0" encoding="UTF-8"?><!--Generated by Ascential Software... (4 Replies)
Discussion started by: pred55
4 Replies

6. Shell Programming and Scripting

Script for adding a word in front of all line in a file

Hi I've one file full of paths of certain files and I want to add some extra file words in front of all the paths. for eg: i have a file name test.txt which show some details only.. 024_hd/044/0344eng.txt 035_bv/222/editor.jpg here I want to add /usr/people/indiana/ infront of all the... (4 Replies)
Discussion started by: ratheeshp
4 Replies

7. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

8. Shell Programming and Scripting

Want to add a word in front a of each line of a file

Hi, Can anybody help me how to add a word in front of a line in a file.Actually it is bit tricky to add a word. i will give a sample for this: Input : 1110001 ABC DEF 1110001 EFG HIJ 1110001 KLM NOP 1110002 QRS RST 1110002 UVW XYZ Output: %HD% 1110001 ABC DEF %DT% 1110001 EFG HIJ... (4 Replies)
Discussion started by: apjneeraj
4 Replies

9. Shell Programming and Scripting

Adding a word in front of a word of each line.

Adding a word in front of a word of each line.In that line only one word will be there. pl help:( (4 Replies)
Discussion started by: Ramesh Vellanki
4 Replies

10. UNIX for Dummies Questions & Answers

Append 0 for single digit entered from command line

I have a script like this-- #!/bin/ksh echo "To pad a 0 before digits from 1-9" for i in $* do echo $i | sed 's//'0'/g' done I run this script as ksh name 1 2 23 34 The output should be 01 02 23 34 Help me in modifying this script. Thanks Namish (2 Replies)
Discussion started by: namishtiwari
2 Replies
Login or Register to Ask a Question