LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-22-2013, 11:11 AM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
Inserting in at a specific column (not delimited)


Hey,
I need to use a bash script that will insert a . at a specific column

Code:
INPUT

TJSDFNT 324329543059300
Code:
OUTPUT
TJSDFNT 32432954.3059300
This is just a sample as I have to do it to hundreds of files each of which is thousands of lines long

I tried using awk FS but I dont know how to define no delimiter so that it will use the absolute column number (similar to the cut -c use)



Thanks
 
Old 02-22-2013, 11:32 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
In awk you can use one of the following:
Code:
-F ""
FS=""
There is a space after the -F prior to the quotes.
 
Old 02-22-2013, 12:38 PM   #3
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
I couldnt get it to work with either

should I even be using awk in the first place, because thats just going to get me a column number I guess then Id have to figure out how to insert a . at that specific column.
 
Old 02-22-2013, 01:11 PM   #4
Liam Mapson
LQ Newbie
 
Registered: Feb 2008
Location: Southwestern PA, US
Distribution: Tiny Core Linux
Posts: 5

Rep: Reputation: 12
If the format of every line is exactly the same, why not just use cut -c ?

Code:
for FILE in your_file_collection ; do {
  echo -n "${FILE} "
  cat "${FILE}" |while read REC ; do {
    PART1="`echo "${REC}" |cut -c1-16`"
    PART2="`echo "${REC}" |cut -c17-23`"
    echo "${PART1}.${PART2}"  >>${FILE}.NEW
    echo -n "."
  } done
  echo ""
} done
The above code is untested and may contain typos, invalid assumptions and/or logical and syntax errors. It is intended only to illustrate a technique.

Last edited by Liam Mapson; 02-22-2013 at 01:44 PM. Reason: wrapped ${} around FILE in line beginning with " cat"
 
1 members found this post helpful.
Old 02-22-2013, 01:36 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Have ...
Code:
TJSDFNT 324329543059300
QWERTYI 314159265358979
EXAMPLE 123456789012345
Want ...
Code:
TJSDFNT 32432954.3059300
QWERTYI 31415926.5358979
EXAMPLE 12345678.9012345
Try cut and paste as shown ...
Code:
 cut -c1-16 $InFile                 \
|paste -d"." - <(cut -c17- $InFile) \
> $OutFile
Daniel B. Martin
 
1 members found this post helpful.
Old 02-22-2013, 01:48 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Have ...
Code:
TJSDFNT 324329543059300
QWERTYI 314159265358979
EXAMPLE 123456789012345
Want ...
Code:
TJSDFNT 32432954.3059300
QWERTYI 31415926.5358979
EXAMPLE 12345678.9012345
Try awk as shown ...
Code:
awk '{print substr($0,1,16)"."substr($0,17)}' $InFile > $OutFile
Daniel B. Martin
 
Old 02-22-2013, 01:59 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Have ...
Code:
TJSDFNT 324329543059300
QWERTYI 314159265358979
EXAMPLE 123456789012345
Want ...
Code:
TJSDFNT 32432954.3059300
QWERTYI 31415926.5358979
EXAMPLE 12345678.9012345
Try sed as shown ...
Code:
sed -r 's/(.{16})(.*)/\1.\2/' $InFile > $OutFile
Daniel B. Martin
 
Old 02-22-2013, 02:16 PM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
a slightly shorter awk:

Code:
awk 'BEGIN{FS=OFS=""}$16=$16"."'
 
2 members found this post helpful.
Old 02-22-2013, 03:03 PM   #9
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by danielbmartin View Post
Have ...

Code:
awk '{print substr($0,1,16)"."substr($0,17)}' $InFile > $OutFile
Daniel B. Martin
Awesome thanks, this worked perfectly
 
Old 02-22-2013, 04:47 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
Code:
awk 'BEGIN{FS=OFS=""}$16=$16"."'
A good definition of technical elegance:
"Completeness of function coupled with economy of means."

Your awk is elegant!

Daniel B. Martin
 
  


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
[SOLVED] Inserting text at specific line X column coordinates udiubu Programming 11 08-07-2012 09:11 AM
[SOLVED] Subtracting the value of a specific cell to the whole column udiubu Programming 13 11-11-2011 10:50 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
Help needed - How to check quality of a specific column in a tab-delimited file? Jason7449 Linux - Newbie 3 03-08-2010 09:36 AM
column re-alignment - space delimited to comma delimited hattori.hanzo Linux - Newbie 9 03-05-2009 12:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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