Trouble using awk and sed commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble using awk and sed commands
# 1  
Old 01-02-2014
Trouble using awk and sed commands

Hi i have a control file which i need to read. It is ',' separated. the 3rd parameter will be ';' separated.
I have 2 files:
/home/orig.txt
/home/join.txt
I need a O/P file name based on firstparameter_1.txt and it should have the content of /home/orig.txt
and appended content from /home/join.txt where it finds keywords from last parameter.
While copying all the content from orig to O/P fiel it should check for any entries of keywords from last parameter and delete whole line if there is an entry
Lines should be appeneded immediately after it finds 'record' key word.
Code:
controlfile.txt
---------------
boy,/home/orig.txt,/home/join.txt,ravi;phani

conetent of /home/orig.txt
---------------------------
record
he is good
he is bad
end
conetent of /home/join.txt
--------------------------
record
ravi is boy
phani is boy
apurva is girl
pushpa is girl
sneha is girl
end
O/P
___
file name:boy_a.txt
content of boy_a.txt:
---------------------
record
ravi is boy
phani is boy
he is good
he is bad
end

__________________________________________________________________
Code:
controlfile.txt
---------------
girl,/home/orig.txt,/home/join.txt,apurva;pushpa;sneha
conetent of  /home/orig.txt
----------------------------
record
he is good
he is bad
end
conetent of /home/join.txt
---------------------------
record
ravi is boy
phani is boy
apurva is girl
pushpa is girl
sneha is girl
end
O/P:
___
file name:girl_a.txt
content of girl_a.txt:
-----------------------
record
apurva is girl
pushpa is girl
sneha is girl
he is good
he is bad
end

The last parameter frm the control file can vary from 1 to n and it will be ; separated if it has greater than 2 entries.
Code:
while read name,orig,append,paralist
do
cp /home/orig.txt $name_a.txt
echo "$paralist" > /home/temporary.txt
awk -F";" '{
for (i=1; i<NF; i++) 
{
cat echo $orig | grep -i $i >> /home/temp.txt
sed -ni '/\($i\)/p' ${orig}
}
}' /home/temporary.txt
sed -i '/record/r /home/temp.txt' $name_a.txt
done<controlfile.txt

# 2  
Old 01-02-2014
Based on assumptions:
Code:
awk '
        BEGIN {
                FS = "[ ,]"
        }
        NR == FNR {
                F = $1
                n = split ( $NF, R, ";" )
                for ( i = 1; i <= n; i++ )
                        C[R[i]] = F
                next
        }
        FILENAME == "join.txt" {
                if ( $1 in C )
                        O[++c] = $0
                next
        }
        FILENAME == "orig.txt" {
                if ( $0 ~ /record/ )
                {
                        print > F "_a.txt"
                        for ( i = 1; i <= c; i++ )
                                print O[i] > F "_a.txt"
                }
                else
                        print > F "_a.txt"
        }
' controlfile.txt join.txt orig.txt

# 3  
Old 01-02-2014
Try
Code:
awk -F, '               {FN=$1"_a.txt"
                         NN=split ($NF, NAMES, ";")
                         getline X < $2; print X > FN
                         while (getline X < $3) for (i=1; i<=NN; i++) if (match (X, NAMES[i]".*"$1)) print X > FN
                         while (getline X < $2)  print X > FN
                        }
        ' controlfile
record
apurva is girl
pushpa is girl
sneha is girl
he is good
he is bad
end

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Trouble looping commands

Hi, I have encountered a problem that I am unable to find a workaround for. I have 52 numbers and I need to submit an individual job for each pair combination, so too many to do by hand. I have created a submission file (submission_code.sh) which contains the following code: gcta64... (2 Replies)
Discussion started by: tim.morris
2 Replies

2. Shell Programming and Scripting

awk or sed or any commands to stripe data

Say i have: g_gateway domain="abc.com" to="123.123.123.123" relay="false" how do i use awk or sed or any method to get the result below: abc.com 123.123.123.123 (3 Replies)
Discussion started by: timmywong
3 Replies

3. Shell Programming and Scripting

Trouble passing commands with expect

Hello All, I hope someone could help me with this. I'm creating a shell script to run a process. The trouble is, part of the process has to be ran as a different user. I can 'su' to the user ok, but I'm having trouble passing a 'cd' command as well as some variables I set earlier in the... (1 Reply)
Discussion started by: bbbngowc
1 Replies

4. UNIX for Dummies Questions & Answers

Ps commands trouble

Hi, i am writing a bash file and need to list the command name and user for the busiest process ie the one using the greatest percentage of CPU time) im kind of stuck. i know you should get the highest CPU , sort column 3 then take off the head but i'm un sure how would i do that? echo... (2 Replies)
Discussion started by: ryoukii
2 Replies

5. Shell Programming and Scripting

Trouble executing piped shell commands in perl code

I am trying to execute a piped combination of shell commands inside a perl program. However, it is not working as desired. This is my program, i am trying to print only filenames from the output of ls -l $ cat list_test #!/usr/bin/perl -w use strict; my $count=0; my @list=`ls -l|awk... (4 Replies)
Discussion started by: sam05121988
4 Replies

6. Shell Programming and Scripting

Variables into SED or AWK and multiple commands

Hello I am hoping you may help. I am not sure how to go about this exactly, I know the tools but not sure how to make them work together. I have two SED commands that I would like to run in a shell script. I would like to take the manual input of a user (types in when prompted) to be used... (4 Replies)
Discussion started by: lostincashe
4 Replies

7. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

8. Shell Programming and Scripting

Have to learn sed and awk commands in kSH?

Links Please??? (2 Replies)
Discussion started by: Diddy
2 Replies

9. Linux

Adding a prefix to a column using awk/sed commands

Hello, I am a newbie to linux and struggling to find a better way to append a column in a text file. Here is the file i want to modify: It has 8 columns (and thousands of rows). I want to append the first column by adding "chr" infront of the numbers. Some rows have a string in the first... (4 Replies)
Discussion started by: bjorngill
4 Replies

10. Shell Programming and Scripting

Using variables within awk/sed commands

Can I use my own variables within awk and sed for example: I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file. I want to use : sed '1s/$/texthere/g' data.csv Like this: sed '$is/$/$age/g' data.csv... (5 Replies)
Discussion started by: mustaine85
5 Replies
Login or Register to Ask a Question