add a hyphen every 2 characters of every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add a hyphen every 2 characters of every line
# 1  
Old 10-28-2008
add a hyphen every 2 characters of every line

I have a text file like this with hundreds of lines:
>cat file1.txt
1027123000
1027124000
1127125000
1128140000
1228143000
>

all lines are very similar and have exactly 10 digits. I want to separate the digits by twodigit and hyphens....like so,
>
10-27-12-30-00
10-27-12-40-00
11-27-12-50-00
11-28-14-00-00
12-28-14-30-00
>

I just did some searching on this forum and it looks like I'll need a command that takes on this form:
sed "s/\([0-9]\......"
I need the exact notation though?
# 2  
Old 10-28-2008
Code:
sed 's/\(..\)\(..\)\(..\)\(..\)\(..\)/\1-\2-\3-\4-\5/' file

# 3  
Old 10-28-2008
thxyou...

thank you. wow yu make it look easy....
then if it is not too much trouble I also need a command that will add a "." between the first 4 numbers and the last 6 numbers....
>cat file1.txt
1027123000
1027124000
1127125000
1128140000
1228143000
>
ends up looking like this:
>cat file1.txt
1027.123000
1027.124000
1127.125000
1128.140000
1228.143000
>

I'm sure I can figure it out using yoru example, but easier to ask! :-)
thank you so much!!!
-A
# 4  
Old 10-28-2008
Hammer & Screwdriver Another approach

Code:
>cat file13
1027123000
1027124000
1127125000
1128140000
1228143000
> awk '{print(substr($0,1,4))"."(substr($0,5,6))}' file13
1027.123000
1027.124000
1127.125000
1128.140000
1228.143000
>

# 5  
Old 10-28-2008
For the first:

Code:
perl -i~ -pe's/(..)(?!.?$)/$1-/g' infile

For the second:

Code:
perl -i~ -pe's/(.{4})/$1./' infile

# 6  
Old 10-29-2008
Code:
sed 's/\(..\)/&-/g' file | sed 's/-$//'

# 7  
Old 10-29-2008
Code:
sed 's/\(.\{2\}\)/&-/g;s/-$//' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove first 2 characters and last two characters of each line

here's what im trying to do. i have a file containing lines similar to this: data.txt: 1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU 1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

3. Shell Programming and Scripting

Grep command to ignore line starting with hyphen

Hi, I want to read a file line by line and exclude the lines that are beginning with special characters. The below code is working fine except when the line starts with hyphen (-) in the file. for TEST in `cat $FILE | grep -E -v '#|/+' | awk '{FS=":"}NF > 0{print $1}'` do . . done How... (4 Replies)
Discussion started by: Srinraj Rao
4 Replies

4. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. UNIX for Dummies Questions & Answers

Line & File Manipulation - add spaces between characters

Is there an awk, sed, vi or any line command that adds Field Separators (default spaces) to each line in a file? $cat RegionalData 12FC2525MZLP8266900216 12FC2525MZLP8266900216 12FC2525NBLP8276900216 12FC2525NBLP8276900216 Desired results: 1 2 F C 2525 MZ LP 826 690 02 16 1 2 F C... (2 Replies)
Discussion started by: MS75001
2 Replies

7. UNIX Desktop Questions & Answers

How to remove text in each line after hyphen?

Hi, I'm trying to do something relatively simple. I have a txt file that has the following kinds of lines (and many more lines): CP19 Oahu - Maunawili Falls CP20 Oahu - Maunawili Falls AG12 Oahu - Maunawili Falls CP22 Oahu - Maunawili Falls, Local area AG14 Oahu CP141 KZ102 Kauai -... (7 Replies)
Discussion started by: euspilapteryx
7 Replies

8. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

9. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies

10. Shell Programming and Scripting

use of hyphen in #! line

In one script i have seen - in #! line can somebody explain the meaning of -(hyphen) here #! /bin/sh - (7 Replies)
Discussion started by: Dhruva
7 Replies
Login or Register to Ask a Question