Sponsored Content
Top Forums Shell Programming and Scripting read string, check string length and cut Post 302111316 by sb008 on Tuesday 20th of March 2007 07:09:32 PM
Old 03-20-2007
Quote:
Originally Posted by ozzy80
Hello All,

Plz help me with:

I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it is.

Example:

csv file:

1,Test Name,"This is a test and is funny",,,1234

Value1: max(10)
Value2: max(8)
Value3: max(21)
Value4: max(5)
Value5: max(5)
Value6: max(5)

and the expected result is:

1,Test Nam,This is a test and is,,,1234

Plz help!

Thnx in advance!
~Ozzy
Code:
echo '1,Test Name,"This is a test and is funny",,,1234' | sed -e 's/"//g' -e 's/\([^,]\{0,10\}\)[^,]*,\([^,]\{0,8\}\)[^,]*,\([^,]\{0,21\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*/\1,\2,\3,\4,\5,\6/'

Code:
sed -e 's/"//g' -e 's/\([^,]\{0,10\}\)[^,]*,\([^,]\{0,8\}\)[^,]*,\([^,]\{0,21\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*,\([^,]\{0,5\}\)[^,]*/\1,\2,\3,\4,\5,\6/' <file>

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

2. Shell Programming and Scripting

Read string from a file,plz help me to check

#!/usr/bin/ksh exec 0<property while read newReceiveDir do if then sed -e 's//home/joshua/bodi/data/receive/{$newReceiveDir:25}/g/' mp_validate.sh >| mp_validate.sh elif then sed -e 's//home/joshua/bodi/data/temp/{$newReceiveDir:22}/g/' mp_validate.sh >| mp_validate.sh ... (1 Reply)
Discussion started by: joshuaduan
1 Replies

3. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

4. Shell Programming and Scripting

how to cut a string from a string

now I have a string .a/b/c/done I want to get "done" from it. That is the result should be "done" sorry for my language before. Thanks in advance (5 Replies)
Discussion started by: Henryyy
5 Replies

5. Shell Programming and Scripting

Read File and check records for length

I need a script that will run in unix to: 1) Read and input file with 1 column that contains for ex: 0123456789 1234567890 ...etc 2) Checks the first column if it is: a. Numeric from 0 - 9 b. if it is not less... (4 Replies)
Discussion started by: mrn6430
4 Replies

6. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

7. UNIX for Dummies Questions & Answers

I need a Script to read Log string and check date at the same time

I need to check 1 log file, which is logging: 2014-08-18T09:10:39+02:00 user: XXXXX START FEATURE 2014-08-18T09:10:39+02:00 user: XXXXX FINISH FEATURE I first need to check that the START FEATURE starts and finish on the same time/date for the same user, which is different each time START... (2 Replies)
Discussion started by: TheBest43
2 Replies

8. Shell Programming and Scripting

How to read file, and replace certain string with another string?

Hi all, the value in the following file is just an example. It could be a different value/network addresses. Here is my example of initial output in a file name net.txt Initial Output, net.txt The goal is to produce the following format which is to convert from CIDR to Netmask... (6 Replies)
Discussion started by: type8code0
6 Replies

9. Shell Programming and Scripting

Script to count matched string then check again from last read position

Hi all, I am currently trying to figure out how can i capture a particular string from a log file, then search again but from the last line it read before onward. Basically let's say that the check runs every 5 mins via cron, counting the number of matched strings "Cannot assign requested... (5 Replies)
Discussion started by: nms
5 Replies
Test::Portability::Files(3)				User Contributed Perl Documentation			       Test::Portability::Files(3)

NAME
Test::Portability::Files - Check file names portability VERSION
Version 0.05 SYNOPSIS
use Test::More; eval "use Test::Portability::Files"; plan skip_all => "Test::Portability::Files required for testing filenames portability" if $@; options(all_tests => 1); # to be hyper-strict run_tests(); DESCRIPTION
This module is used to check the portability across operating systems of the names of the files present in the distribution of a module. The tests use the advices given in "Files and Filesystems" in perlport. The author of a distribution can select which tests to execute. To use this module, simply copy the code from the synopsis in a test file named t/portfs.t for example, and add it to your MANIFEST. You can delete the call to "options()" to enable only most common tests. By default, not all tests are enabled because some are judged too cumbersome to be practical, especially since some of the most limited platforms (like MS-DOS) seem to be no longer supported. Here are the default options: o "use_file_find" is not enabled (check only the names as listed in MANIFEST) o "test_amiga_length" is enabled o "test_ansi_chars" is enabled o "test_case" is enabled o "test_dos_length" is not enabled o "test_mac_length" is enabled o "test_one_dot" is enabled o "test_space" is enabled o "test_special_chars" is enabled o "test_symlink" is enabled o "test_vms_length" is enabled To change any option, please see "options()". EXPORT
The following functions are exported: o "options()" o "run_tests()" FUNCTIONS
"options()" Set the module options, in particular, select which tests to execute. Expects a hash. General options o "use_file_find" - set to 1 to check all the files in the current hierarchy using "File::Find" instead of only checking files listed in MANIFEST. Tests o "all_tests" - select all tests. o "test_amiga_length" - check that the name fits within AmigaOS name length limitations (107 characters). o "test_ansi_chars" - check that the name only uses the portable filename characters as defined by ANSI C and recommended by perlport. o "test_case" - check that the name of the file does not clash with the name of another file on case-insensitive filesystems. o "test_dir_noext" - check that the directory has no extension. o "test_dos_length" - check that the name fits within DOS name length limitations (8 characters max for the base name, 3 characters max for the extension). o "test_mac_length" - check that the name fits within Mac OS Classic name length limitations (31 characters). o "test_one_dot" - check that the name only has one dot. o "test_space" - check that the name has nos space. o "test_special_chars" - check that the name does not use special characters. o "test_symlink" - check that the file is not a symbolic link. o "test_vms_length" - check that the name fits within VMS name length limitations (39 characters max for the base name, 39 characters max for the extension). Example options(use_file_find => 1, all_tests => 1); selects all tests and runs them against all files found using "File::Find". "test_name_portability()" Test the portability of the given file name. "run_tests()" Execute the tests selected by "options()". SEE ALSO
perlport AUTHOR
Sebastien Aperghis-Tramoni, <sebastien@aperghis.net> BUGS
Please report any bugs or feature requests to "bug-test-portability-files@rt.cpan.org", or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. COPYRIGHT &; LICENSE Copyright 2004 Sebastien Aperghis-Tramoni, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2004-11-21 Test::Portability::Files(3)
All times are GMT -4. The time now is 11:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy