Sponsored Content
Top Forums Shell Programming and Scripting Help with Creation of Script to Input Separators in Data Post 302323995 by Alexis Duarte on Tuesday 9th of June 2009 02:36:11 PM
Old 06-09-2009
Quote:
Originally Posted by Franklin52
This command should place comma's after 2 then after 5,7 and 10 positions, so you should get columns of 2, 5, 7, .. positions:

Code:
awk -v p="2,5,7,10" '
BEGIN { n=split(p,pos,",");b=1 }
{ for(i=1;i<=n;i++) {
    printf("%s%c",substr($0,b,pos[i]),",")
    b+=pos[i]
  }
  print substr($0,b)
}' file

Thanks Franklin for the solution, but while this brings me closer to solving everything to me, there are still some lose ends.
This code you provided me with, only applies to the first line, and it doesn´t stop where I want it.
So I made some changes, but i still have this problem with first line:
The code changed by me:

Code:
#!/bin/bash
#script to separate the fields in MEPS file
for x in `cat $1`
do awk '$1~/^2/ {print}' $1 |awk -v p="1,2,4,8,8,6,10,5,1,10,5,15,9,1,1" '
BEGIN { n=split(p,pos,",");b=1 }
{ for(i=1;i<=n;i++) {
    printf("%s%c",substr($0,b,pos[i]),",")
    b+=pos[i]
  }
  print substr($0,b)
}'
done



This produces the following result:
Code:
2,04,2173,00073846,20090507,144018,0000010000,00000,A,0000000202,83139,Praia          ,000000016,0,0,
 ,  ,    ,     ,,,,,,,,,,,,
2,04,2173,00073846,20090507,144018,0000010000,00000,A,0000000202,83139,Praia          ,000000016,0,0,
 ,  ,    ,     ,,,,,,,,,,,,
2,04,2173,00073846,20090507,144018,0000010000,00000,A,0000000202,83139,Praia          ,000000016,0,0,
 ,  ,    ,     ,,,,,,,,,,,,
2,04,2173,00073846,20090507,144018,0000010000,00000,A,0000000202,83139,Praia          ,000000016,0,0,
 ,  ,    ,     ,,,,,,,,,,,,
2,04,2173,00073846,20090507,144018,0000010000,00000,A,0000000202,83139,Praia          ,000000016,0,0,
 ,  ,    ,     ,,,,,,,,,,,,
2,04,2173,00073846,20090507,144018,0000010000,00000,A,0000000202,83139,Praia          ,000000016,0,0,


So you see, that it repeats the first line and it produces these series of ,,, ,,, ,,, ,,,,, that I do not want.
I tried to modified the code many times, but I think the problem is that I did not understant all of the code.
So what I´m asking is for you to give me some pointer on how to make this work and some explanation on the original code you provided me.

Thanks in advance.

Last edited by Alexis Duarte; 06-09-2009 at 04:28 PM.. Reason: Forgot the code tags. Sorry about that
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creation of script,if the data file have more than one entry!!!

1.Daily there will be 14 files in the data directory 2.someday's the 14 files receive more than once r twice with different time stamps....we need to chk the count of the file and if the count of the file is two.we need to combine the both the files. 3. if any duplicate data is there just... (1 Reply)
Discussion started by: bobprabhu
1 Replies

2. Shell Programming and Scripting

Multiple input field Separators in awk.

I saw a couple of posts here referencing how to handle more than one input field separator in awk. I figured I would share how I (just!) figured out how to turn this line in a logfile: 90000000000000000000010001 name... (4 Replies)
Discussion started by: kinksville
4 Replies

3. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

4. Shell Programming and Scripting

Script creation (Input needed)

Hi , I m trying to write a script in linux . The problem is the output of command get changed every now and then. Just like top command in linux. How would i manipulate the output of the command ? The command i m talking about gives the real time values of performance of hosted guest... (3 Replies)
Discussion started by: pinga123
3 Replies

5. Shell Programming and Scripting

Echo date variable from data input to a script

Hi, I'm trying to make a script which you type the year, select the month and day and then create the date in the format 2010-12-7. #!/bin/bash dia () { echo " Seleccione el dia:" select file in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Salir do... (6 Replies)
Discussion started by: iga3725
6 Replies

6. Shell Programming and Scripting

adding data in input file if 2nd script output SUCCESS

Hi All, how can i edit my original data and add more data if my 2nd script outputs SUCESS? ex. input file: 1.txt nik,is,the 1script.sh if 2ndscript.sh output SUCCESS then i'm going to edit my input file and add data best,pogi.. sample outputdata. nik,is,the,best,pogi 2ndscript.sh... (3 Replies)
Discussion started by: nikki1200
3 Replies

7. Shell Programming and Scripting

Script asks to input data

Hi, I have three different files about a warehouse's stock status. Each file shows storage locations, stored product names, quantity of the part and at last column, its price. When there is a change in price, I open those files one by one, search related product name at each row and change... (4 Replies)
Discussion started by: baris35
4 Replies

8. Shell Programming and Scripting

Standardization of input source data files using shell script

Hi there, I'm a newbie in unix and am fishing for options related to how raw input data files are handled. The scenario, as I'm sure y'all must be very familiar with, is this : we receive upwards of 50 data files in ASCII format from various source systems - now each file has its own structure... (3 Replies)
Discussion started by: Prat Khos
3 Replies

9. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

10. Shell Programming and Scripting

Parsing out data with multiple field separators

I have a large file that I need to print certain sections out of. file.txt /alpha/beta/delta/gamma/425/590/USC00015420.blah.lt.0.01.str:USC00015420Y2017M10BLALT.01 12 13 14 -9 1 -9 -9 -9 -9 -9 1 2 3 4 5 -9 -9 I need to print the "USC00015420" and... (5 Replies)
Discussion started by: ncwxpanther
5 Replies
CUT(1)							    BSD General Commands Manual 						    CUT(1)

NAME
cut -- select portions of each line of a file SYNOPSIS
cut -b list [-n] [file ...] cut -c list [file ...] cut -f list [-d delim] [-s] [file ...] DESCRIPTION
The cut utility selects portions of each line (as specified by list) from each file and writes them to the standard output. If the file argument is a single dash ('-') or no file arguments were specified, lines are read from the standard input. The items specified by list can be in terms of column position or in terms of fields delimited by a special character. Column numbering starts from 1. list is a comma or whitespace separated set of increasing numbers and/or number ranges. Number ranges consist of a number, a dash (-), and a second number and select the fields or columns from the first number to the second, inclusive. Numbers or number ranges may be preceded by a dash, which selects all fields or columns from 1 to the first number. Numbers or number ranges may be followed by a dash, which selects all fields or columns from the last number to the end of the line. Numbers and number ranges may be repeated, overlapping, and in any order. It is not an error to select fields or columns not present in the input line. The options are as follows: -b list The list specifies byte positions. -c list The list specifies character positions. -d string Use the first character of string as the field delimiter character. The default is the <TAB> character. -f list The list specifies fields, separated by the field delimiter character. The selected fields are output, separated by the field delimiter character. -n Do not split multi-byte characters. -s Suppresses lines with no field delimiter characters. Unless specified, lines with no delimiters are passed through unmodified. EXIT STATUS
cut exits 0 on success, 1 if an error occurred. SEE ALSO
paste(1) STANDARDS
The cut utility conforms to IEEE Std 1003.2-1992 (``POSIX.2''). BSD
December 21, 2008 BSD
All times are GMT -4. The time now is 05:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy