LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-23-2012, 03:40 AM   #1
Linuxahoy
LQ Newbie
 
Registered: Oct 2011
Location: India
Distribution: Slackware 12
Posts: 4

Rep: Reputation: Disabled
Question Conditional editing of a file through Shell script


Hi,

I am trying to write a small shell script. The purpose of the script is...
I have a tabulated data in a file as follows.
Serial No USN field1 field2 field3 field4 Name
1 794 1.83 1.83 0.00 3.67 con
2 686 0.00 0.00 0.00 0.00 abc
3 883 0.00 0.00 0.00 0.00 xyz
4 872 0.92 0.00 0.00 0.92 lmn
5 705 0.00 0.00 0.00 0.00 cat
6 628 0.92 4.59 0.00 5.50 etc
7 958 0.00 0.00 0.00 0.00 lop
8 957 0.00 0.00 0.00 0.00 nam
9 956 0.00 0.00 0.00 0.00 mak
10 955 0.00 0.00 0.00 0.00 lab

the size of the file will be several hundred lines. The script should read the file and if a particular value is found in the "USN" column(Value will be provided through a variable), then the content of last column "Name" should be changed.
i.e if $usn is the variable containing the search value, I want to find if $usn is present in the "USN" column. If yes, I want to replace the last column of that line with a value present in another variable $name.
Ex:
$usn=628
$name=ham
since 628 is present in 6th line, the "Name" column data of that line should be replaced with ham.

Please let me know how I can achieve this. I am not able to figure out if awk can be used here.
Please help.
 
Old 01-23-2012, 04:33 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
You should be able to do this using awk:
http://www.grymoire.com/Unix/Awk.html

You can pass the variable in and then use awk to test one column and change another column.
 
Old 01-23-2012, 04:45 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

A working solution:
Code:
#!/bin/bash

if [[ $# != 2 ]]
then
  echo "Please provide correct input..."
  exit 1
fi

usn=$1
newName=$2

awk -v usn="$usn" -v newName="$newName" '
  { if ( $2 == usn ) { $7 = newName } }
  { print $0 }
' infile
It is up to you to figure out how this works

Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
editing file with script B-Boy Programming 4 09-02-2009 08:23 AM
Editing Ascii File Using Shell Script rohit2983 Programming 7 01-03-2007 06:27 AM
Editing text file in Shell - Konsole (SuSE 9.1) Ree Linux - Newbie 5 10-03-2004 11:48 AM
Editing a text file in shell Lotmr Linux - Software 5 06-24-2003 12:12 PM
ID3 Tag Editing with a Shell Script chrisk5527 Linux - General 5 03-20-2003 10:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration