How to extract every 6-th character in a line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract every 6-th character in a line?
# 1  
Old 11-20-2018
How to extract every 6-th character in a line?

Hi, I have a file consist of 25000 lines and each line has 2 columns 1 column with 6 numeric characters and 2nd one with 45000 numeric characters (not delimited). I want to take every 7th character form the 2nd column, keeping the first column. I made it in several steps but it run for 7 hours to solution. Does anyone has faster solution? Thanks!

Code:
example
File dd0.txt 

100001 1100000000011000011000012222111111111100000000000001122222112221111110012222122222220100000000 ... up to 45000
100002 1100000000011000011000012222111111111100000000000001122222112221111110012222122222220100000000 ... up to 45000

125000 1100000000011000011000012222111111111100000000000001122222112221111110012222122222220100000000 ... up to 45000


result
100001 10001100220120 ... up to 6429
100002 10001100220120 ... up to 6429

125000 10001100220120 ... up to 6429


Last edited by Neo; 11-20-2018 at 08:13 AM..
# 2  
Old 11-20-2018
Hmm. Sounds not that complicated. Would you like to show us your solution?
# 3  
Old 11-20-2018
Your desired result doesn't seem to match your specification. Shouldn't it be more like
Code:
100001 10001100210200... 
100002 10001100210200...
125000 10001100210200...

?
# 4  
Old 11-20-2018
... I'm curious how fast the awk solutions will be.

Here the test data generator (about 1 GB Data):

Code:
#!/bin/bash

tr -dc '0-9' </dev/urandom | fold -w45000 | while read line ;do 
        ((c=$c+1))
        [[ $c -gt 25000 ]] && break
        echo $(($c + 100000)) $line 
done

This User Gave Thanks to stomp For This Post:
# 5  
Old 11-20-2018
Hi,
Maybe with sed below but with same resultat give by RudiC:
Code:
sed -e 's/.\{7\}\|.\{1,6\}$/_&/g;s/^_//;s/_\(.\)[0-9]*/\1/g' file

Regards.
This User Gave Thanks to disedorgue For This Post:
# 6  
Old 11-21-2018
Hi, thanks for reply it is much faster, I tested even on much bigger files it is working fine.

------ Post updated at 03:24 AM ------

It seem that is not working properly.
# 7  
Old 11-21-2018
Not much that we can work with. In WHAT way above "is not working properly"? And, please answer post#2!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

2. Shell Programming and Scripting

Extract First character in Second column

Hi, I need to extract the first character of second column of my file. If the condition matches, then I need to print the 2nd and 3rd column as my output I tried below mentioned query but it was not working awk -F'|' '$2~/^5/' Sgn_group.txt File Name : Sgn_group.txt country... (2 Replies)
Discussion started by: suresh_target
2 Replies

3. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

4. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. Shell Programming and Scripting

Extract a character specifying position

hi , i am having a file Full_ARTMAS_20110510152425.xml in my local directory. i wanted to extract the character at the 35143546 th position at line 1 of this file.Can any body help me how to do it??? regards Anjali (2 Replies)
Discussion started by: angel12345
2 Replies

7. Shell Programming and Scripting

Extract character between specific line numbers

Hi guys, I have txt file and I would need to extract all the contents between specific line numbers. Line 1: apple Line 2: orange Line 3: mango Line 4: grapes Line 5: pine apple I need to extract the content between line 2 and 4, including the contents of Line 2 and 4 so the ouput... (2 Replies)
Discussion started by: gowrishankar05
2 Replies

8. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

9. Shell Programming and Scripting

extract character + 1

Hi, I would like extract from a file a character or pattern after ( n + 1) a specific pattern (n) . ( i supposed with awk) how could i do ? Thanks in advance. (1 Reply)
Discussion started by: francis_tom
1 Replies

10. UNIX for Advanced & Expert Users

Extract a character

HI Guys, Have a Doubt...... I have a pattern "abcdef" and i need to extract the third character..ie(c) How to achieve it? (10 Replies)
Discussion started by: aajan
10 Replies
Login or Register to Ask a Question