Visit Our UNIX and Linux User Community


Bash script to count and insert


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to count and insert
# 1  
Old 08-27-2013
Bash script to count and insert

Hi not sure if this is possible but I need some help with a bash script, I have a text file and on the first line that starts with 7150230 I need it to put a 1 at position 79 and a 2 at position 88, this is where it gets complicated, on the next line it finds that starts with 7150230 I then need it to put a 3 at position 79 and a 4 at position 88, next line that starts with 7150230 a 5 at position 79 and a 6 at position 88 and so on.

Below is an example of what I need it to do

Code:
71502304541-000            B1                    000000000000300100000001800001        2                                    S   

71502304540-000            B2                    000000000000300100000000000003        4

71502304541-000            B1                    000000000000300100000001800005        6                                    S   

71502304540-000            B2                    000000000000300100000000000007        8

# 2  
Old 08-27-2013
Your input data is like this?
Code:
71502304541-000            B1                    00000000000030010000000180000                                              S
some data
71502304540-000            B2                    00000000000030010000000000000
some more data
here are more
71502304541-000            B1                    00000000000030010000000180000                                              S
71502304540-000            B2                    00000000000030010000000000000


Then you could do
Code:
awk '/7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, 7) "" ++a substr($0, 89)};'
71502304541-000            B1                    000000000000300100000001800001       2                                    S
71502304540-000            B2                    000000000000300100000000000003       4
71502304541-000            B1                    000000000000300100000001800005       6                                    S
71502304540-000            B2                    000000000000300100000000000007       8

# 3  
Old 08-27-2013
Quote:
Originally Posted by Jotne
Your input data is like this?
Code:
71502304541-000            B1                    00000000000030010000000180000                                              S
some data
71502304540-000            B2                    00000000000030010000000000000
some more data
here are more
71502304541-000            B1                    00000000000030010000000180000                                              S
71502304540-000            B2                    00000000000030010000000000000

Then you could do
Code:
awk '/7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, 7) "" ++a substr($0, 89)};'
71502304541-000            B1                    000000000000300100000001800001       2                                    S
71502304540-000            B2                    000000000000300100000000000003       4
71502304541-000            B1                    000000000000300100000001800005       6                                    S
71502304540-000            B2                    000000000000300100000000000007       8


Hi Jotne,

There are other lines of data so this way won't work, unless there's a way to keep those lines in?

Thank you for the help, it is much appreciated
# 4  
Old 08-27-2013
You may retain remaining lines using

Code:
awk '/^7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, 7) "" ++a substr($0, 89)} !/^7150230/ {print}' inputfile

# 5  
Old 08-27-2013
@firefox2k2

Post a much larger file (complete if its possible)
# 6  
Old 08-27-2013
Quote:
Originally Posted by krishmaths
You may retain remaining lines using

Code:
awk '/^7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, 7) "" ++a substr($0, 89)} !/^7150230/ {print}' inputfile

Hi that's kind of working problem is when it gets into double digits it's moving everything after up a place, like its inserting an extra character rather than copying over. I have highlighted the problem below, thanks again for all the help.

Code:
71402994728-100            Y700/0                0060000000540000ST0000051300000ST    001                S        G             
71502304541-000            B1                    000000000000300100000001800001        2                                    S   
71502304540-000            B2                    000000000000300100000000000003        4                                        
7130200619798130823CV10003    5500038441        024                  024                                                        
71402994729-100            Y701/0                0060000000540000ST0000052920000ST    001                S        G            
71502304541-000            B1                    000000000000300100000001800005        6                                    S   
71502304540-000            B2                    000000000000300100000000000007        8                                        
7130200619799130823CV10003    5500038442        024                  024                                                        
71402994730-100            Y702/0                0060000000600000ST0000052800000ST    001                S        G             
71502304541-000            B1                    000000000000500100000001200009        10                                    S   
71502304540-000            B2                    0000000000005001000000000000011        12 

# 7  
Old 08-27-2013
Test if a>=10, use different value.
@krishmaths no need for print at the end, its default action.
Code:
awk '/7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, a>=10?6:7) "" ++a substr($0, a>=10?89:88)} !/^7150230/' file


Previous Thread | Next Thread
Test Your Knowledge in Computers #1009
Difficulty: Easy
The number 128 in base 10 equals 64 in base 16.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python Script to take file count and insert into DB2 table

Team I have files in different directories . How can i take the count of latest file and insert into Db2 table . I am using awk 'END{print NR+1-ARGC}' (File name) to get the counts. How can i take 1.The count of latest file 2.Insert into Db2 table( File Name and Counts) . cd... (4 Replies)
Discussion started by: Perlbaby
4 Replies

2. UNIX for Beginners Questions & Answers

BASH SCRIPT - Insert date into cells in cvs file

Hi, I'm looking to accomplish the following. Insert current date into three places/cells within a cvs, every time the bash script is executed. The cells are column A,B,C row 2. Row 1 is reserved for the headers. The file name is always orders.csv. These three cells we always have an old... (1 Reply)
Discussion started by: Rookievmc
1 Replies

3. Shell Programming and Scripting

Linux shell script to insert new lines based on delimiter count

The input file is a .dat file which is delimited by null (^@ in Linux). On a windows PC it looks something like this (numbers are masked with 1). https://i.imgur.com/nta2Gqp.jpg The entire file is in one row but it has multiple records - each record contains 80 fields i.e. there are 81 counts... (9 Replies)
Discussion started by: digitalnirvana
9 Replies

4. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F '' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

5. Shell Programming and Scripting

Help with insert a value equation in bash script

HI All, I have a script in bash that i want that script will perform action When the size of a particular folder exceeds the 80%. Here is an example of script that result is exactly 80% : #!/bin/bash CHECK=$(df -h /var/log/syslog | grep '80%' | xargs echo | cut -d' ' -f5) if ];... (1 Reply)
Discussion started by: Aviel.shani
1 Replies

6. Shell Programming and Scripting

bash script search file and insert character when match found

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (0 Replies)
Discussion started by: firefox2k2
0 Replies

7. UNIX for Dummies Questions & Answers

Bash script to insert data into an html table

hi, I need to create a bash shell script which picks up data from a text file and in the output file puts it into an html made table. I have to use sed and awk utilties to do this the input text file will contain data in the format: job name para1 para2 para3 para4 para4 1 ... (1 Reply)
Discussion started by: intern123
1 Replies

8. Shell Programming and Scripting

count and compare no of records in bash shell script.

consider this as a csv file. H,0002,0002,20100218,17.25,P,barani D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 T,5 N i want to read the csv file and count the number of rows that start with D and... (11 Replies)
Discussion started by: barani75
11 Replies

9. Shell Programming and Scripting

insert leading zeroes based on the character count

Hi, I need add leading zeroes to a field in a file based on the character count. The field can be of 1 character to 6 character length. I need to make the field 14bytes. eg: 8351,20,1 8351,234,6 8351,2,0 8351,1234,2 8351,123456,1 8351,12345,2 This should become. ... (3 Replies)
Discussion started by: gpaulose
3 Replies

10. Shell Programming and Scripting

bash script to count the time of transaction

Halo, Bash Script can get the time of process the trasaction or not? For example, bash script use to procee the trasaction, like select and checking.. then generate the XML. after it, i need to get the time which to count the process. Anyone can help me? Thank you (1 Reply)
Discussion started by: ryanW
1 Replies

Featured Tech Videos