Sponsored Content
Top Forums Shell Programming and Scripting Search and recursively enter new line after Nth character Post 302889067 by wisecracker on Tuesday 18th of February 2014 02:02:18 PM
Old 02-18-2014
An example using builtins only, OSX 10.7.5, default shell...
Just a 256 byte ascii file with a newline every 16 bytes...
Code:
#!/bin/sh
# Generate a random 256 byte ASCII string and prepare.
> /tmp/ascii
> /tmp/newascii
echo ""
echo "Generating a 256 byte ASCII test string..."
for n in {0..255}
do
	printf \\$(printf '%03o' "$[ ( $RANDOM % ( 95 ) + 32 ) ]") >> /tmp/ascii
done
echo "Done..."
echo ""
read -d '' -r text < /tmp/ascii
echo "$text"
echo ""
echo "Now longhand using builtins..."
echo ""
# Set up and preparation done...
# Create newlines every 16 characters...
# Start of conversion proper...
newtext=""
for n in $( seq 0 16 255 )
do
		echo "$newtext${text:$n:16}" >> /tmp/newascii
done
# End of conversion proper...
# Proof test...
cat < /tmp/newascii
echo ""

Results:-
Code:
Last login: Tue Feb 18 18:52:41 on ttys000
AMIGA:barrywalker~> chmod 755 nl_16.sh
AMIGA:barrywalker~> ./nl_16.sh

Generating a 256 byte ASCII test string...
Done...

+Cax7Om%D[y2Pg'>\s3Jh!?Vu-Kc"@Wv.Ld#:Yp/Fe|;Sq)H_}6Tk*B`w7Nl%CZy1Om&D[z2Ph'>]t3Ji!?Wu-Lc"9Xo.Fd{;Rp)Ge|<Sq*H_~6Tl+B`x7Nm%C[y1Pg&=\s2Jh ?Vt4Ki!@Wu.Lc#:Xo/Fd|;Rq)G_}5Sk*A`w6Nl+Cax7Om%D[y2Pg'>\s3Jh!?Vu-Kc"9Wo.Ed{:Yp/Fe|;Sq)H_}6Tk*B`w7Nl%CZy1Of&=[s2Ph'>]t3Ji!?

Now longhand using builtins...

+Cax7Om%D[y2Pg'>
\s3Jh!?Vu-Kc"@Wv
.Ld#:Yp/Fe|;Sq)H
_}6Tk*B`w7Nl%CZy
1Om&D[z2Ph'>]t3J
i!?Wu-Lc"9Xo.Fd{
;Rp)Ge|<Sq*H_~6T
l+B`x7Nm%C[y1Pg&
=\s2Jh ?Vt4Ki!@W
u.Lc#:Xo/Fd|;Rq)
G_}5Sk*A`w6Nl+Ca
x7Om%D[y2Pg'>\s3
Jh!?Vu-Kc"9Wo.Ed
{:Yp/Fe|;Sq)H_}6
Tk*B`w7Nl%CZy1Of
&=[s2Ph'>]t3Ji!?

AMIGA:barrywalker~> _

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

HELP with Enter Character

I'm using PHP in my site. I have txt files for news. I reads the txt files and post the news. i have a php function that finds the caracter asc(13) which is the enter, and puts a <br>. It works in Windows, but not on Unix. Can u help me with that. Thanks (1 Reply)
Discussion started by: zecarlos
1 Replies

2. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

3. Shell Programming and Scripting

Read file from nth line to specific character

Hi, I want to read the file from nth line (where n is an integer) to until I encounter @ char. Can any one please help me how to do this? Thanks. (3 Replies)
Discussion started by: laalesh
3 Replies

4. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

5. Shell Programming and Scripting

Search for a string, then append character to end of that line only

I have 2 files that I am working with $ cat file1 server1 server3 server5 server6 server8 $ cat file2 server1;Solaris; server2; SLES; server3;Linux; server4; Solaris; server5;SLES; server6;SLES; server7;Solaris; server8;Linux; (1 Reply)
Discussion started by: snoman1
1 Replies

6. UNIX for Dummies Questions & Answers

Appending a character(#) with string search at the start of the line

Hello, I have been browsing through the forum, but unable to find a solution for my requirement. I need to go through a file and search for /home/users and insert a # symbol at the start /home/users. Example output is #/home/users. Can you please help me with the awk or sed command for... (1 Reply)
Discussion started by: chandu123
1 Replies

7. Shell Programming and Scripting

Perl - start search by using search button or by pressing the enter key

#Build label and text box $main->Label( -text => "Input string below:" )->pack(); $main->Entry( -textvariable => \$text456 )->pack(); $main->Button( -text => "Search", -command => sub { errchk ($text456) ... (4 Replies)
Discussion started by: popeye
4 Replies

8. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

9. Shell Programming and Scripting

Replace nth to nth character?

Hi I got the following problem and I wonder if some could please help me out? I'd like to replace character 8 - 16 , 16 - 24 cat file ... (2 Replies)
Discussion started by: stinkefisch
2 Replies

10. Post Here to Contact Site Administrators and Moderators

How to read the nth character from the line.?

I have Index Line and I tried to get the 9th character from the file and to check the character is "|" or not. Shell Scripting. Sample Index file. "91799489|K8E|188.004.A.917994892.1099R.c.01.pdf|2013|10/15/2014|002|B|C|C"... (3 Replies)
Discussion started by: pavand
3 Replies
curs_ins_wstr(3X)														 curs_ins_wstr(3X)

NAME
ins_wstr, ins_nwstr, wins_wstr, wins_nwstr, mvins_wstr, mvins_nwstr, mvwins_wstr, mvwins_nwstr - insert a wide-character string into a curses window SYNOPSIS
#include <curses.h> int ins_wstr(const wchar_t *wstr); int ins_nwstr(const wchar_t *wstr, int n); int wins_wstr(WINDOW *win, const wchar_t *wstr); int wins_nwstr(WINDOW *win, const wchar_t *wstr, int n); int mvins_wstr(int y, int x, const wchar_t *wstr); int mvins_nwstr(int y, int x, const wchar_t *wstr, int n); int mvwins_wstr(WINDOW *win, int y, int x, const wchar_t *wstr); int mvwins_nwstr(WINDOW *win, int y, int x, const wchar_t *wstr, int n); DESCRIPTION
These routines insert a wchar_t character string (as many characters as will fit on the line) before the character under the cursor. All characters to the right of the cursor are shifted right, with the possibility of the rightmost characters on the line being lost. No wrap- ping is performed. The cursor position does not change (after moving to y, x, if specified). The four routines with n as the last argu- ment insert a leading substring of at most n wchar_t characters. If n is less than 1, the entire string is inserted. If a character in wstr is a tab, newline, carriage return or backspace, the cursor is moved appropriately within the window. A newline al- so does a clrtoeol before moving. Tabs are considered to be at every eighth column. If a character in wstr is another control character, it is drawn in the ^X notation. Calling win_wch after adding a control character (and moving to it, if necessary) does not return the con- trol character, but instead returns a character in the ^-representation of the control character. NOTES
Note that all but wins_nwstr may be macros. If the first character in the string is a nonspacing character, these functions will fail. XSI does not define what will happen if a non- spacing character follows a control character. RETURN VALUES
Upon successful completion, these functions return OK. Otherwise, they return ERR. SEE ALSO
curses(3X), curs_insstr(3X), curs_in_wch(3X), curs_ins_wch(3X). curs_ins_wstr(3X)
All times are GMT -4. The time now is 10:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy