LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2004, 04:54 PM   #1
Irmo
LQ Newbie
 
Registered: Dec 2004
Posts: 4

Rep: Reputation: 0
Script in c shell


I need a script which if a file contains on first row a string ver.1 change this string to a ver.2.
Whit another word's a script which update the version of file which is written on the first row in file. So if there is a ver.1 it change it to ver. 2, if there is a ver.x it change it to a ver.(x+1). It's not normal that file contains in first row this ver., but my files contains and I need a automatically update.
I'm a beginner in linux :-(.
 
Old 12-15-2004, 04:56 PM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
theres a shell command called expr that will help you.

man expr

End
 
Old 12-15-2004, 04:56 PM   #3
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Have a look at Sed and tr - either will probably do what you wish.
 
Old 12-15-2004, 05:03 PM   #4
Irmo
LQ Newbie
 
Registered: Dec 2004
Posts: 4

Original Poster
Rep: Reputation: 0
the problem is I don't't know how to upgrade the number to a number + 1 (how to read the number from the file) a example of a script will be a help.
 
Old 12-15-2004, 07:14 PM   #5
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Well, it would probably take more than one line for Sed (you could use a modified version of this incrementing script, perhaps).

Otherwise, you could use the venerable Perl, in a similar manner to this post. For example, use something along the lines of perl -pe 's/(ver.)(\d)(.)/$1.($2+1).$3/eg' \ <somefile>.

Cheers,

mj
 
Old 12-16-2004, 04:14 AM   #6
Irmo
LQ Newbie
 
Registered: Dec 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Unhappy

so this shoud be a script to encreas a number, but have can I change this script that it encraese number in a first row of a file aa.txt. I try to do it for at least 1 hour but I wasn't sucesfull.

#!/usr/bin/sed -f

:d
s/9\(_*\)$/_\1/
td

# if there aren't any digits left, add a MostSign Digit 0
#
s/^\(_*\)$/0\1/

# incr last digit only - there is no need for more
#
s/8\(_*\)$/9\1/
s/7\(_*\)$/8\1/
s/6\(_*\)$/7\1/
s/5\(_*\)$/6\1/
s/4\(_*\)$/5\1/
s/3\(_*\)$/4\1/
s/2\(_*\)$/3\1/
s/1\(_*\)$/2\1/
s/0\(_*\)$/1\1/

# replace all _ to 0s
#
s/_/0/g
 
Old 12-16-2004, 04:50 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
There are a bunch of commands that can compute numbers with unix, including expr, bc, dc, awk and modern shells too.
Why using sed alone, which has no clue about numerics ?
 
Old 12-16-2004, 05:06 AM   #8
Irmo
LQ Newbie
 
Registered: Dec 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Can you pleas write a example of script which find a number in first row of a file and then it increase it by one.

If file look something like this.
aa.txt

sdjgkd 34 lsdfjsdflk
ljsf dfsfj d;fkd fsj
fdk jf 55 5 345

Then I run the script and the file aa.txt whould look like

sdjgkd 35 lsdfjsdflk
ljsf dfsfj d;fkd fsj
fdk jf 55 5 345

(just the 34 will be increased to 35)
 
Old 12-16-2004, 02:41 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Assuming the numerical field is the second one:
Code:
awk '
NR == 1 {  $2=$2+1 }
{ print $0 }
' aa.txt
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 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