Solved

Truncate last 40 chars of every line in a file

Posted on 1998-12-02
3
223 Views
Last Modified: 2010-04-21
I am wondering if there is some function to turncate the last 40 chars of every line in a given file. And also, when I execute the system command like "system(". .profile");" in a C program, the profile is not executed.
0
Comment
Question by:bujji120198
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 30 total points
ID: 2008191
awk '{l=length($0);if(l>40){print substr($0,1,l-40)}}' file

How do you proove that .profile was not executed?
I suggest adding following to your .profile:
echo "called at `date` by $user" > /tmp.profile

Then tell us what the error is, or what does not work.
0
 
LVL 1

Expert Comment

by:toTo
ID: 2008192
another suggestion

"system(". ./.profile");"
0
 
LVL 3

Expert Comment

by:mliberi
ID: 2008193
about "truncating the first 40 characters":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do you need a C or shell function?

if C you can use strcpy()
if shell I suggest to use the 'typeset' internal command.

Example (ksh):
let's suppose we want to get the first 40 chars from var 'line'

typeset -L40 L40=$line

echo "$L40"   # first 40 chars, eventually padded with blanks

about "system(". .profile")":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I agree with ahoffmann, your .profile should get executed, but
if your purpose was to define and/or export variables, functions,
aliases and so on for sure you failed!

In fact when you call the 'system' C library function your process forks a shell that executes the command ". .profile". All the variables you define there remains in the subprocess environment and are lost when the subprocess exit, just before the 'system' function returns.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question