LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-21-2009, 07:14 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Rep: Reputation: 32
Modifying records of a file using shell scripting


$cat file
a 1 2 3
b 2 3 4
c 3 4 5
d 4 5 6

I need to write a script which will read the records of file and increment the 2nd field by 5 and write it back.

$cat script.sh

echo "file name:"
read file

if [ -f $file ]
then
exec 0<$file

while read line
do
#set `cat $line| tr -s " "`
#t=`expr $2 + 1`
f1=`echo $line | cut -d' ' -f1`
f2=`echo $line | cut -d' ' -f2`
f2=`expr $f2 + 5`
f3=`echo $line | cut -d' ' -f3`
f4=`echo $line | cut -d' ' -f4`
echo "$f1 $f2 $f3 $f4" >>tmp
done
mv tmp $file
fi



But
echo $f1 $f2 $f3 $f4 >>tmp, will not work if we have say 100 fields.
i.e writing
echo "$1 $2 .... $100" >>aa

is not a good programming.

Is there any alternate and better way to do so.
 
Old 05-21-2009, 08:08 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A simple awk one-liner can do the trick:
Code:
awk '{sub($2,($2 + 5),$2)}1' file
To answer to your question you can try extended brace expansion to get the sequence of numbers from 1 to 100:
Code:
while read line
do
  #
  #  Read fields from 1 to 100
  #
  for i in {1..100}
  do
    eval "f${i}=$(echo $line | cut -d' ' -f$i)"
  done
  #
  #  Change second field
  #
  f2=$(($f2 + 5))
  #
  #  echo them all
  #
  eval "echo \$f{1..100}"
done
 
Old 05-21-2009, 08:36 AM   #3
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
I think these brases does not work in my bash.
{1..100}
 
Old 05-21-2009, 08:48 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This means you have a BASH version older than 3. You can try the seq command instead:
Code:
while read line
do
  #
  #  Read fields from 1 to 100
  #
  for i in $(seq 1 100)
  do
    eval "f${i}=$(echo $line | cut -d' ' -f$i)"
  done
  #
  #  Change second field
  #
  f2=$(($f2 + 5))
  #
  #  echo them all
  #
  eval echo $(seq -f"\$f%.0f" 1 100)
done

Last edited by colucix; 05-21-2009 at 09:02 AM.
 
Old 05-21-2009, 09:09 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '{$2+=5}1'  file
 
Old 05-21-2009, 09:33 AM   #6
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
Now it is working.
Thanks a lot for your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing value to a file using shell scripting Keshav murthy Programming 4 09-25-2008 04:26 AM
Shell script for modifying file extensions VTGuitarMan Linux - Newbie 6 03-08-2008 07:24 PM
file formatting via shell scripting athreyavc Programming 2 10-09-2007 03:55 AM
modifying a file in shell (via sed ?) Shautieh Programming 7 09-19-2006 05:14 AM
Shell scripting from java file karthiknataraj Linux - General 1 02-08-2003 04:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:04 AM.

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