Help with File processing - Adding predefined text to particular record based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with File processing - Adding predefined text to particular record based on condition
# 1  
Old 10-12-2011
Help with File processing - Adding predefined text to particular record based on condition

I am generating a output:
Code:
Name Count_1 Count_2
abc       12       12
def        15      14
ghi        16       16
jkl         18       18
mno      7          5

I am sending the output in html email, I want to add the code:
<font color="red"> NAME COLUMN record </font>
for the Name column records when the Count_1 & Count_2 are different. If they are equal no need to add the HTML tags. ( just to differentiate from others)

For eg:
In the above example, I have to add the tag to def and mno records.
# 2  
Old 10-12-2011
awk is meant for stuff like this. one-liner:

Code:
awk 'NR==1 { print } NR>1 { if($2 != $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

with more explanation:

Code:
awk '
# If we're on the first line, just print it unmodified.
NR==1 { print }
# If we're on other lines:
 NR>1 {
        # If the second record doesn't match the third, print the entire line($0) surrounded by tags.
        if($2 != $3) printf("<font color="red">%s</font>\n", $0);
        else print;        # Otherwise, print the entire line unmodified.
}' < infile


Last edited by Corona688; 10-12-2011 at 12:35 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-12-2011
Code:
#!/usr/bin/ksh
while read mName mCnt1 mCnt2; do
  if [[ "${mCnt1}" != "${mCnt2}" ]]; then
    echo '<font color="red">'${mName}'</font>' ${mCnt1} ${mCnt2}
  else
    echo ${mName} ${mCnt1} ${mCnt2}
  fi
done < Inp_File

This User Gave Thanks to Shell_Life For This Post:
# 4  
Old 10-12-2011
Quote:
Originally Posted by Corona688
awk is meant for stuff like this. one-liner:

Code:
awk 'NR==1 { print } NR>1 { if($2 != $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

It worked... but it was making $2 and $3 also red.

After processing it was appending as:
Code:
<font color=red> def         15      14</font>

# 5  
Old 10-12-2011
What do you want, then? Your <font color="red"> NAME COLUMN record </font> seemed to imply you wanted all three columns red...

---------- Post updated at 10:20 AM ---------- Previous update was at 10:18 AM ----------

If just the first column, then:
Code:
awk 'NR==1 { print } NR>1 { if($2 != $3) $1 = "<font color="red">" $1 "</font>"; print; }' < infile

# 6  
Old 10-14-2011
Question

Quote:
Originally Posted by Corona688
awk is meant for stuff like this. one-liner:

Code:
awk 'NR==1 { print } NR>1 { if($2 != $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

with more explanation:
The same I need in $2>$3 condition, so I put :
Code:
awk 'NR==1 { print } NR>1 { if($2 -gt $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

It is not working... All the records are effected for this... but there are only some which satisfy this condition.
# 7  
Old 10-14-2011
awk doesn't use -gt.

Code:
awk 'NR==1 { print } NR>1 { if($2 > $3) printf("<font color="red">%s</font>\n", $0); else print; }' < infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Manipulate condition to send mail based on output text in file

Hi All, I have a working script as below. echo "Files loaded with $(cat /var/tmp/script.X1.out)" | mail -s "Files loaded with return code" mailid This script takes the output from script.X1.out file and appends the text "Files loaded with return code" and sends the email. Now what I want... (5 Replies)
Discussion started by: midhun3108
5 Replies

2. Shell Programming and Scripting

Adding data from a file based on some condition

I collect data in a file in below format(Month Day Year Size) in RedHat Linux. Now i want to calculate the data size date wise. As i code shell script after long time, i forgot the features and syntax. Can anyone help me regard this please. Feb 8 2014 15 Feb 10 2014 32 Feb 10 2014 32 Feb 12... (2 Replies)
Discussion started by: makauser
2 Replies

3. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

4. UNIX for Dummies Questions & Answers

Adding a column to a text file based on mathematical manipulation

Hi, I have a tab delimited text file with three different columns. I want to add an extra column to the text file. The extra column will be the second column and it will equal third column - 1. How do I go about doing that? Thanks! Input: chr1 788822 rs11240777 chr1 1008567 rs9442372... (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Replacing the text in a row based on certain condition

Hi All, I felt tough to frame my question. Any way find my below input. (.CSV file) SNo, City 1, Chennai 2, None 3, Delhi 4,None Note that I have many rows ans also other columns beside my City column. What I need is the below output. SNo, City 1, Chennai 2, Chennai_new 3, Delhi... (2 Replies)
Discussion started by: ks_reddy
2 Replies

6. Shell Programming and Scripting

multiple condition matching and doing some predefined work

Hi everybody, I had 10 files in in one folder(/home/sai/) namely sai. 1.gz,2.gz,3.gz ..,10.gz. I want to delete the files which are there home based on the following conditions fliecount in sai folder==10 && grep -cv ".gz"==0 How to check this using awk? Otherwise please... (2 Replies)
Discussion started by: p_sai_ias
2 Replies

7. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

Adding a column to a text based on file name

Dear all, Does anyone know how I could to add a column of numbers (1s, or 2s, or..., or 6s) to two-column text files (tab-delimited), where the specific number to be added varies as a function of the file naming? Currently, each of my text files has two columns, so the column with the... (12 Replies)
Discussion started by: rlapate
12 Replies

10. Shell Programming and Scripting

splitting a record and adding a record to a file

Hi, I am new to UNIX scripting and woiuld appreicate your help... Input file contains only one (but long) record: aaaaabbbbbcccccddddd..... Desired file: NEW RECORD #new record (hardcoded) added as first record - its length is irrelevant# aaaaa bbbbb ccccc ddddd ... ... ... (1 Reply)
Discussion started by: rsolap
1 Replies
Login or Register to Ask a Question