Hopefully a simple script, bash or perl...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hopefully a simple script, bash or perl...
# 1  
Old 01-06-2011
Hopefully a simple script, bash or perl...

I'm attempting to parse a file whose contents follow this format;
Code:
4:/eula.1028.txt:
  8:/eula.1031.txt:
  19:/eula.1033.txt:
  23:/eula.1036.txt:
  27:/eula.1040.txt:
  31:/eula.1041.txt:
  35:/eula.1042.txt:
  39:/eula.2052.txt:
  43:/eula.3082.txt:

The number of lines of the file will vary but the format is constant; #:TEXT:

What I would like is for the script to subtract the numerical variables of a line (starting with line 1 and 2) from the line above it, -3 then associate that new value with the above TEXT and output the results to a new file. An example of the formula I have in mind would be expressed something like this;

Line #2 = 8
Line #1 = 4
So; (8 - 4) = 4 - 3 = 1

THEN

Line #3 = 19
Line #2 = 8
So; (19 - 8) = 11 - 3 = 8

And so on until it reaches the end of the file.

For example, the results of the new file should look like this;
Code:
1:/eula.1028.txt:
  8:/eula.1031.txt:
1:/eula.1033.txt:
  1:/eula.1036.txt:
  1:/eula.1040.txt:
1:/eula.1041.txt:
  1:/eula.1042.txt:
  1:/eula.2052.txt:

Line 43:/eula.3082.txt: would not be able to be figured as there is no following line to subtract from and the script should end.

I've been attempting to utilize bash, awk, sed, to get this done but it's just eluding me. I'm not a perl programmer at all but I suspect there is likely a simple way to perform this function in perl, but in bash I keep chasing my tail.

Thanks in advance for any help on this one!

Last edited by Franklin52; 01-07-2011 at 03:29 AM.. Reason: Please use code tags.
# 2  
Old 01-06-2011
awk will do this with two pass through the file.
In 1st pass, just store 1st field (FS=: )in array i.e. a[NR]=$1
In 2nd pass, just apply the formula as below: $1=a[FNR+1]-a[FNR]-3
# 3  
Old 01-06-2011
Thanks for the quick reply!

I've attempted to implement the awk command with the code suggestions you mention but I keep getting a syntax error, probably my clueless ability at coding, I assume this can't be done from the command line?

Can you show me an explicit sinp of the syntax used to perform multiple passes on the file with awk?

Thanks a ton for the help!

---------- Post updated at 03:33 PM ---------- Previous update was at 03:25 PM ----------

I took your answer litterally as you can see here;
Code:
#cat input.file | awk -FS: a[NR]=$1 | awk $1=a[FNR+1]-a[FNR]-3 > Result.txt
awk: cmd. line:1: a[NR]=
awk: cmd. line:1:       ^ unexpected newline or end of string
awk: =a[FNR+1]-a[FNR]-3
awk: ^ syntax error

I'm sure this is just me being a doofus.

Last edited by Franklin52; 01-07-2011 at 03:30 AM.. Reason: code tags
# 4  
Old 01-06-2011
Code:
awk -F: 'NR>1{print ($1-a[1])-3,a[2],X}{split($0,a,":")}' OFS=: infile

# 5  
Old 01-06-2011
Works perfectly! Thank you!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert bash to simple perl

please delete! (0 Replies)
Discussion started by: SkySmart
0 Replies

2. Shell Programming and Scripting

Covert simple bash script in perl language

Hello, Anyone please covert this in perl language ######################## if ps faux | grep -v grep | grep ProcessXYZ then echo "$SERVICE is running, , everything is fine" exit 0 else echo "$SERVICE is not running" exit 2 fi Additional... (1 Reply)
Discussion started by: fed.linuxgossip
1 Replies

3. Shell Programming and Scripting

Very simple script using bash language

Dear AIXians, I have a task for very simple script, but I can't write it correctly :confused: I have a file called (out.txt), if any line of this file starts with the word 'ORA', it must send email, if the file (out.txt) don't have this word, so it do nothing. Please tell me how. ... (4 Replies)
Discussion started by: Mohannad
4 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. Shell Programming and Scripting

Simple bash script help

Hi to everyone here, I'm a new user and relatively-new linuxer. I'm trying to write a script that checks if every file from a directory is present in a given list and if not, delete it. should be simple. But I think I've done half the work only: this is to create the reference list: for c... (2 Replies)
Discussion started by: dentex
2 Replies

6. Shell Programming and Scripting

need a simple bash script

to gather the cpu utilization from a system in 5 minute intervals and direct output to file. I'm new at scripting and while this seems like an easy task I'm confused on where to start. thanks for any help (1 Reply)
Discussion started by: mkeyes001
1 Replies

7. Shell Programming and Scripting

Simple BASH script not working?

So I need a script that does the following: If a certain user is logged in Run `command` Else Echo “incorrect user” This is my first stab...which doesn't work: #!/bin/bash X="user=`ls -l /dev/console | cut -d " " -f 4`" Y="foobar" echo $X echo $Y (4 Replies)
Discussion started by: doubleminus
4 Replies

8. Shell Programming and Scripting

simple bash script

I am writing a shell script in bash one of the thing I want to show is size of export /home du -sk /export/home/oracle | cut -c 1-5 echo "kbytes" when I run the script kbytes shows up in the second line, How can I append kbytes on the same line, such as 61233 kbytes please guide thanks (2 Replies)
Discussion started by: Tirmazi
2 Replies

9. Shell Programming and Scripting

Simple BASH script?

Hi guys, I'm new to the forum so forgive me if I'm sounding ... daft. I currently work in a Tech Support role. Every day we have to generate data by running around 10 .sh scripts. I was thinking instead of having to ./filename 10 times is it possible to right a new script that will run these for... (16 Replies)
Discussion started by: JayC89
16 Replies

10. Shell Programming and Scripting

Simple Bash Script

I'm sure I'm doing something wrong but as I am new to bash shell scripting I'm not sure what: Here's the code webalizer.conf is sitting in the same directory as this file which is named webalizer.sh. Can someone tell me if I've got the syntax right -- it that's correct? I'm executing the... (3 Replies)
Discussion started by: xaphalanx
3 Replies
Login or Register to Ask a Question